Question

Is there an SPL Reverse array iterator in PHP? And if not, what would be the best way to achieve it?

I could simply do

$array = array_reverse($array);
foreach($array as $currentElement) {}

or

for($i = count($array) - 1; $i >= 0; $i--)
{

}

But is there a more elegant way?

Was it helpful?

Solution

There is no ReverseArrayIterator to do that. You can do

$reverted = new ArrayIterator(array_reverse($data));

or make that into your own custom iterator, e.g.

class ReverseArrayIterator extends ArrayIterator 
{
    public function __construct(array $array)
    {
        parent::__construct(array_reverse($array));
    }
}

OTHER TIPS

Here is a solution that does not copy and does not modify the array:

for (end($array); key($array)!==null; prev($array)){
  $currentElement = current($array);
  // ...
}

If you also want a reference to the current key:

for (end($array); ($currentKey=key($array))!==null; prev($array)){
  $currentElement = current($array);
  // ...
}

This works always as php array keys can never be null and is faster than any other answer given here.

$item=end($array);
do {
...
} while ($item=prev($array));

Depending on what you are trying to do, you might want to look into the spl data structure classes, such as SplStack. SplStack implements Iterator, ArrayAccess and Countable, so it can mostly be used like an array, but by default, its iterator proceeds in FILO order. Ex:

$stack = new SplStack();
$stack[] = 'one';
$stack[] = 'two';
$stack[] = 'three';

foreach ($stack as $item)
{
    print "$item\n";
}

This will print

three
two
one

Based on linepogl's answer, I came up with this function:

/**
 * Iterate an array or other foreach-able without making a copy of it.
 *
 * @param array|\Traversable $iterable
 * @return Generator
 */
function iter_reverse($iterable) {
    for (end($iterable); ($key=key($iterable))!==null; prev($iterable)){
        yield $key => current($iterable);
    }
}

Usage:

foreach(iter_reverse($my_array) as $key => $value) {
    // ... do things ...
}

This works on arrays and other iterables without first making a copy of it.

Note that if you want to preserve the keys of the array, you must pass true as the second parameter to array_reverse:

$array = array_reverse($array, true);
foreach ($array as $currentElement) {
    // do something here
}
$array = array_reverse($array);
foreach($array as $key => $currentElement) {}

This is better way to use. It will take care of keys also, if they are not sequential or integer.

Based on linepogl's answer... You can make it even more efficient by avoiding current() call

for ($value = end($array); ($key = key($array)) !== null; $value = prev($array)) {
     // ... do something with $key => $value
}
$array=array(
    0 => 0,
    '1' => 1,
    2 => null,
    3 => false
);

$value=end( $array );                      // ← value for first iteration
while(($key=key( $array )) !== null) {
  echo "key=$key, value=$value\n";

  $value=prev( $array );                   // ← value for next iteration
}

This could be a more performant way since it doesnt construct a new array. It also handles empty arrays well.

$item = end($items);
while($item)
{
    ...do stuff...
    $item = prev($items);
}

$array1= array(10,20,30,40,50);

        for($i = count($array1) - 1; $i >= 0; $i--)
        {
            $array2[]  = $array1[$i];

        }

        echo "<pre>";
            print_r($array2);
        echo "</pre>";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top