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