猿问

PHP - 组合来自 multipe while 循环的输出

我需要返回一个结果,该结果组合了通过多个 WHILE 循环从多个数组创建的语句。


我有四个数组,并试图根据条件获得不同的输出。这些数组都由彼此相同的键构成,键对应于一个事件。其中一个数组的每个键可能有多个值,如果该特定数组包含多个值,则需要检查该特定数组中的值数量以使用不同的输出结构。


foreach ($closures as $closure) {


//stuff


    for ( $i = 0; $i < $count; $i++){        

        $vcount = count( $locations[$i] );


        while ( $vcount < 2 ) {

            $result = "The ".$venue[$i]." has been reserved for an <a href=".$eventURLs[$i].">event</a> today from ".$startTime[$i]." - ".$endTime[$i].".<br>";

            //print_r($result); //looks good printed...  

            break;

        }


        while ($vcount > 1 ) {

            $result =  "The ".$venue[$i]."s have been reserved for an <a href=".$eventURLs[$i].">event</a> today from ".$startTime[$i]." - ".$endTime[$i].".<br>";

            //print_r($result); //looks good printed... 

            break;

        }

    }

    return $result;

    break;

}

我已经尝试了“break”的每一种组合,当然它给了我不同的输出,但没有一个是正确的。我尝试为每个 while 循环($result1 和 $result2)分配不同的变量。我试过在循环内外用 $result[$i] 替换 $result 。我试过移动“返回...”的位置。


如图所示使用 print_r 时,它显示完美,但我无法弄清楚如何将这些结果放入单个输出中。


我只能通过我的短代码获得第一个结果输出,并且在尝试将它们组合成如下语句时只能设法获得两个结果(每个循环一个):


$message = "".result1."".result2."";


ibeautiful
浏览 165回答 1
1回答

隔江千里

您可以使用 .= 赋值运算符将结果值相互连接foreach ($closures as $closure) {//stufffor ( $i = 0; $i < $count; $i++){&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $vcount = count( $locations[$i] );&nbsp; &nbsp; // initiate the result&nbsp; &nbsp; $result = "";&nbsp; &nbsp; while ( $vcount < 2 ) {&nbsp; &nbsp; &nbsp; &nbsp; // concatinate the value to $result&nbsp; &nbsp; &nbsp; &nbsp; $result .= "The ".$venue[$i]." has been reserved for an <a href=".$eventURLs[$i].">event</a> today from ".$startTime[$i]." - ".$endTime[$i].".<br>";&nbsp; &nbsp; &nbsp; &nbsp; //print_r($result); //looks good printed...&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }&nbsp; &nbsp; while ($vcount > 1 ) {&nbsp; &nbsp; &nbsp; &nbsp; // concatinate the value to $result again&nbsp; &nbsp; &nbsp; &nbsp; $result .=&nbsp; "The ".$venue[$i]."s have been reserved for an <a href=".$eventURLs[$i].">event</a> today from ".$startTime[$i]." - ".$endTime[$i].".<br>";&nbsp; &nbsp; &nbsp; &nbsp; //print_r($result); //looks good printed...&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }}return $result;break;}
随时随地看视频慕课网APP
我要回答