18

我想将关联数组与 PHP 迭代器一起使用:

http://php.net/manual/en/class.iterator.php

是否可以?

我定义了这些方法:

  public function rewind(){    
    reset($this->_arr);
    $this->_position = key($this->_arr);
  }

  public function current(){    
    return $this->_arr[$this->_position];
  }

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

  public function next(){    
    ++$this->_position;
  }

  public function valid(){    
    return isset($this->_arr[$this->_position]);
  }

问题是它没有正确迭代。我只得到一个元素。

我认为这是因为++$this->_positionnext() 方法中的代码没有任何效果,因为 _position 是一个字符串(关联数组的键)。

那么我怎样才能进入这种数组的下一个元素呢?

4

5 回答 5

34
function rewind() {
    reset($this->_arr);
}

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

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

function next() {
    next($this->_arr);
}

function valid() {
    return key($this->_arr) !== null;
}
于 2012-06-17T21:38:38.157 回答
5

为什么不创建一个ArrayObjectfrom Your associative Array?然后,您可以根据需要getIterator()从中ArrayObject调用key()next()等等...

一些例子:

$array = array('one' => 'ONE', 'two' => 'TWO', 'three' = 'THREE');
// create ArrayObject and get it's iterator
$ao = new ArrayObject($my_array);
$it = $ao->getIterator();
// looping
while($it->valid()) {
    echo "Under key {$it->key()} is value {$it->current()}";
    $it->next();
}

ArrayObject
ArrayIterator

于 2012-06-17T23:46:23.587 回答
2

http://ideone.com/Fxt0j

class myIterator implements Iterator {
    private $position = 0;
    private $keys;

    public function __construct(array $arr) {
        $this->array = $arr;
        $this->keys = array_keys($arr);
        $this->position = 0;
    }

    function rewind() {
        $this->position = 0;
    }

    function current() {
        return $this->array[$this->key()];
    }

    function key() {
        return $this->keys[$this->position];
    }

    function next() {
        ++$this->position;
    }

    function valid() {
        return isset($this->keys[$this->position]);
    }
}

$it = new myIterator(array(
        'a' => "firstelement",
        'b' => "secondelement",
        'c' => "lastelement",
    ));

foreach($it as $key => $value) {
    var_dump($key, $value);
    echo "\n";
}
于 2012-06-17T21:03:36.193 回答
1
class MyItrator implements Iterator 
{
    private $_arr;

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


    public function rewind()
    {
        reset($this->_arr);
    }

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

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

    public function next()
    {
        next($this->_arr);
    }

    public function valid()
    {
        return isset($this->_arr[key($this->_arr)]);
    }
}
于 2014-09-17T09:24:11.063 回答
0

我知道这是一个老问题,但它可以很简单

public function current()
{
    return $this->array[array_keys($this->array)[$this->position]];
}

public function next()
{
    ++$this->position;
}

public function key()
{
    return array_keys($this->array)[$this->position];
}

public function valid()
{
    return array_key_exists($this->position, array_keys($this->array)[$this->position]);
}

public function rewind()
{
    $this->position = 0;
}

我知道@zerkms 发布了类似的答案,但恕我直言,如果对象已经构建并且您具有扩展数组的功能,则该答案不起作用

于 2017-02-06T16:16:41.030 回答