Sebastian Hoitz
Sebastian Hoitz

Reputation: 9393

Iterate in reverse through an array with PHP - SPL solution?

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?

Upvotes: 35

Views: 42153

Answers (11)

linepogl
linepogl

Reputation: 9355

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

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

If you also want a reference to the current key:

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

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

Upvotes: 81

Gordon
Gordon

Reputation: 317167

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));
    }
}

A slightly longer implementation that doesn't use array_reverse but iterates the array via the standard array functions would be

class ReverseArrayIterator implements Iterator
{
    private $array;

    public function __construct(array $array)
    {
        $this->array = $array;
    }

    public function current()
    {
        return current($this->array);
    }

    public function next()
    {
        return prev($this->array);
    }

    public function key()
    {
        return key($this->array);
    }

    public function valid()
    {
        return key($this->array) !== null;
    }

    public function rewind()
    {
        end($this->array);
    }
}

Upvotes: 14

C.M.
C.M.

Reputation: 437

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
}

Upvotes: 6

mpen
mpen

Reputation: 283213

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.

Upvotes: 12

Rupesh
Rupesh

Reputation: 1

$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>";

Upvotes: -2

Tadej Kobe
Tadej Kobe

Reputation: 19

$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
}

Upvotes: 1

bitluni
bitluni

Reputation: 219

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);
}

Upvotes: 0

AbiusX
AbiusX

Reputation: 2404

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

Upvotes: 17

Rob Allen
Rob Allen

Reputation: 12778

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
}

Upvotes: 6

Gaurav
Gaurav

Reputation: 28765

$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.

Upvotes: 5

rr.
rr.

Reputation: 6644

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

Upvotes: 10

Related Questions