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

对着背影说爱祢 提交于 2019-11-27 20:35:20

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

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.

AbiusX
$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
mpen

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>";
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!