Php foreach loop wrapping every 2 items

后端 未结 6 1342
梦谈多话
梦谈多话 2021-02-06 08:03
query(a
6条回答
  •  闹比i
    闹比i (楼主)
    2021-02-06 08:36

    You should use a for loop instead of a foreach loop like so:

    for($i = 0; $i < count($children); $i+=2) {
        $child1 = $children[$i];
        $child2 = $children[$i+1];
        // print both
    }
    

    if you may have an odd number of children you have to check if $i+1 < count($children) before printing it.

提交回复
热议问题