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

后端 未结 11 1861
清酒与你
清酒与你 2020-12-05 13:51

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)         


        
相关标签:
11条回答
  • 2020-12-05 14:21

    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
    
    0 讨论(0)
  • 2020-12-05 14:22

    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-05 14:26

    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
    }
    
    0 讨论(0)
  • 2020-12-05 14:28

    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.

    0 讨论(0)
  • 2020-12-05 14:32

    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.

    0 讨论(0)
  • 2020-12-05 14:32
    $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
    }
    
    0 讨论(0)
提交回复
热议问题