iterate – PHP Blog https://blog.dev-php.site Snippets and guides Sun, 14 Apr 2019 06:49:42 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.4 PHP iterate through an array in reverse https://blog.dev-php.site/php-iterate-through-an-array-in-reverse/ Sat, 06 Apr 2019 06:06:58 +0000 http://blog.dev-php.site/?p=90 To reverse the items in an array you have two methods of achieving this
array_reverse()

$array = [1,2,3,4,5,6,7,8,9];

$rev = array_reverse($array);

foreach($rev as $val) {
    print $val;
}

end() & prev() rewind

   $array = [1,2,3,4,5,6,7,8,9];
    print end($array);

    while($val = prev($array)) {
        print $val;
    }

will both print:
987654321

]]>