The question is simple. I have a foreach loop in my code:
foreach($array as $element) {
//code
}
In this loop, I want to r
You can use an anonymous function, too:
$indexOfLastElement = count($array) - 1;
array_walk($array, function($element, $index) use ($indexOfLastElement) {
// do something
if (0 === $index) {
// first element‘s treatment
}
if ($indexOfLastElement === $index) {
// last not least
}
});
Three more things should be mentioned:
array_values first.$element you have to pass it by reference (&$element).$indexOfLastElement inside the use construct, again by reference if needed.