How do you combine two foreach loops into one

前端 未结 4 1219
故里飘歌
故里飘歌 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:01

    I don't understand what you're trying to do. If you want to reach them one after the other just use two loops:

    foreach ($a as $b) { ... }
    foreach ($c as $d => $e) { ... }
    

    If you want all combinations from $a and $c:

    foreach ($a as $b) {
      foreach ($c as $d => $e) {
        // do stuff
      }
    }
    

    I guess you could do something like:

    foreach (array_merge($a, $c) as $k => $v) {
      ...
    }
    

    but I wouldn't necessarily advise it.

提交回复
热议问题