How do you combine two foreach loops into one

前端 未结 4 1208
故里飘歌
故里飘歌 2020-12-30 11:09

The language is PHP. I have one foreach ( $a as $b) and another foreach ($c as $d => $e). How do i combine them to read as one. I tired foreach (($a as $b) && ($c as

4条回答
  •  北海茫月
    2020-12-30 12:00

    This will do what you want I think. It will be advance both arrays equally at the same time throughout your loop. You can always break manually if $c is a different size than $a and you need breaking logic based on array size:

    foreach($a as $b)
    {
        list($d,$e) = each($c);
        //continue on with $b, $d and $e all set
    }
    

    each() will advance the array pointer of $c on each iteration.

提交回复
热议问题