【问题标题】:How do I copy an ArrayIterator to preserve it's current iteration position?如何复制 ArrayIterator 以保留它的当前迭代位置?
【发布时间】:2013-12-02 19:57:58
【问题描述】:

因为这似乎是我必须做的才能获得这种效果:

$arr = ['a'=>'first', 'b'=>'second', ...];
$iter = new ArrayIterator( $arr );

// Do a bunch of iterations...
$iter->next();
// ...

$new_iter = new ArrayIterator( $arr );
while( $new_iter->key() != $iter->key() ) {
    $new_iter->next();
}

编辑:另外,为了清楚起见,我不应该用unset() 修改基本数组吗?我认为数组迭代器存储自己的基本数组副本,因此使用offsetUnset() 似乎不正确。

【问题讨论】:

    标签: php arrays iterator copy


    【解决方案1】:

    ArrayIterator 没有实现tell() 的功能,但是你可以模拟这个,然后使用seek() 去你想要的位置。这是一个扩展类,它就是这样做的:

    <?php
        class ArrayIteratorTellable extends ArrayIterator {
            private $position = 0;
    
            public function next() {
                $this->position++;
                parent::next();
            }
    
            public function rewind() {
                $this->position = 0;
                parent::rewind();
            }
    
            public function seek($position) {
                $this->position = $position;
                parent::seek($position);
            }
    
            public function tell() {
                return $this->position;
            }
    
            public function copy() {
                $clone = clone $this;
                $clone->seek($this->tell());
                return $clone;
            }
        }
    ?>
    

    用途:

    <?php
        $arr = array('a' => 'first', 'b' => 'second', 'c' => 'third', 'd' => 'fourth');
        $iter = new ArrayIteratorTellable( $arr );
    
        $iter->next();
    
        $new_iter = new ArrayIteratorTellable( $arr );
    
        var_dump($iter->current()); //string(6) "second"
        var_dump($new_iter->current()); //string(6) "first"
    
        $new_iter->seek($iter->tell()); //Set the pointer to the same as $iter
    
        var_dump($new_iter->current()); //string(6) "second"
    ?>
    

    DEMO


    或者,您可以使用自定义copy() 函数来克隆对象:

    <?php
        $arr = array('a' => 'first', 'b' => 'second', 'c' => 'third', 'd' => 'fourth');
        $iter = new ArrayIteratorTellable( $arr );
    
        $iter->next();
    
        $new_iter = $iter->copy();
    
        var_dump($iter->current()); //string(6) "second"
        var_dump($new_iter->current()); //string(6) "second"
    ?>
    

    DEMO

    【讨论】:

      【解决方案2】:

      我想到的唯一解决方案是使用当前数组的副本

      $arr = ['a'=>'first', 'b'=>'second'];
      $iter = new ArrayIterator( $arr );
      // Do a bunch of iterations...
      $iter->next();
      var_dump($iter->current());
      // ...
      $arr2 = $iter->getArrayCopy();
      $new_iter = new ArrayIterator( $arr2 );
      
      while( $new_iter->key() != $iter->key() ) {
          var_dump($new_iter->current());
          $new_iter->next();    
      }
      

      【讨论】:

      • 复制当前数组如何保留其迭代位置?
      • 好像我有点误会了,如果你想保住位置最好的办法就是h2ooooooo提供的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-21
      • 1970-01-01
      • 2019-06-01
      • 2021-05-09
      • 2012-09-24
      • 2017-03-06
      • 2017-07-23
      相关资源
      最近更新 更多