为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
ibeautiful 2021-12-03 15:57:47
我需要返回一个结果,该结果组合了通过多个 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."";
查看完整描述

1 回答

?
隔江千里

TA贡献1906条经验 获得超10个赞

您可以使用 .= 赋值运算符将结果值相互连接


foreach ($closures as $closure) {


//stuff


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

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

    // initiate the result

    $result = "";

    while ( $vcount < 2 ) {

        // concatinate the value to $result

        $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 ) {

        // concatinate the value to $result again

        $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;

}


查看完整回答
反对 回复 2021-12-03
  • 1 回答
  • 0 关注
  • 164 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信