How to determine the first and last iteration in a foreach loop?

前端 未结 20 1311
遇见更好的自我
遇见更好的自我 2020-11-22 16:45

The question is simple. I have a foreach loop in my code:

foreach($array as $element) {
    //code
}

In this loop, I want to r

20条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 17:17

    Not sure if it still necessary. But the following solution should work with iterators and does not require count.

     0) {
                $first = $step == 1;
                list ($key, $value) = $current;
                if (call_user_func($cb, $key, $value, $step, $first, $last) === false) {
                    break;
                }
            }
            if ($last) {
                break;
            }
        }
    }
    

提交回复
热议问题