下一个用于关联数组的迭代器方法


Next Iterator method for associative Array

我想使用带有 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]);
  }

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

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

那么我怎样才能转到此类数组的下一个元素呢?

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

为什么不从您的关联Array创建ArrayObject?然后你可以从这个ArrayObject getIterator(),并根据需要调用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();
}

数组对象
阵列迭代器

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";
}
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)]);
    }
}
我知道

这是一个老问题,但它可以像

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发布了类似的答案,但恕我直言,如果对象已经构造并且您具有扩展数组的功能,则

不起作用