If you don't mind destroying the array (or a temporary copy of it), you can do:
$stack = array("orange", "banana", "apple", "raspberry"); while ($fruit = array_pop($stack)){ echo $fruit . "\n<br>"; }
gives:
raspberry apple banana orange
I think this solution is easier to read than messing around with the index, and you are less likely to introduce index processing errors, but the problem is that your code will most likely take a little longer if you have to create a temporary copy of the array first . Index twisting is likely to work faster, and it can also come in handy if you really need to reference the index, as in:
$stack = array("orange", "banana", "apple", "raspberry"); $index = count($stack) - 1; while($index > -1){ echo $stack[$index] ." is in position ". $index . "\n<br>"; $index--; }
But, as you can see, you have to be very careful with the index ...
Damian Green Jun. 19 '17 at 2:20 2017-06-19 02:20
source share