h1

Reverse foreach() for PHP

May 6, 2011

Just a quick programming tip for PHP newbie programmers (e.g., me)

<?php

foreach(array_reverse($array) as $value) {
//your code goes here...
}

?>

Never knew there was an array_reverse function to begin with. Ah well, RTFM, I guess. Hope this helps; you don’t have to sift through the entire manual and figure that out yourself, huh. You lazy coder who went to Google. 😛

7 comments

  1. I though google was a manual =)


  2. A genuinely lazy coder wouldn’t have even bothered to want to run a foreach in reverse =P googling was so much work to boot!

    Thanks =)


    • Heh. Good point. Thanks for the response! 🙂


  3. thanks for this.


  4. What with objects?


  5. it works thanks a lot for providing knowledge.


  6. Actually, doing an array_reverse() can be a bottleneck for big (and I mean BIG) arrays, so it may make more sense to do end($arr), and then while((list($key, $val) = prev($arr)) !== false){} to iterate.



Comments are closed.

%d bloggers like this: