How do I set my foreach loop to start looking at the last entry of the array then each loop will go backwards instead of forward?
Thank you.
How do I set my foreach loop to start looking at the last entry of the array then each loop will go backwards instead of forward?
Thank you.
You could just reverse the array:
$reverse = array_reverse($array, true); // true to preserve keys
foreach($reverse as $key => $value) { /* etc. */ }
Or if you're sure that the array contains only numeric keys, this is probably faster:
for($i = count($array) - 1; $i >= 0; $i--) {
/* etc. */
}
foreach(array_reverse($array, true) as $key=>$value)
The array_reverse function will reverse an array.