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

时间:2012-06-17 20:56:17

标签: php arrays iterator

我想在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是一个字符串(关联数组的键)。

那我怎么去这个类型的数组的下一个元素呢?

5 个答案:

答案 0 :(得分:30)

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

答案 1 :(得分:5)

为什么不从关联ArrayObject创建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(); } 等等。

一些例子:

{{1}}

ArrayObject
ArrayIterator

答案 2 :(得分: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";
}

答案 3 :(得分: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)]);
    }
}

答案 4 :(得分: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发布了一个类似的答案,但是如果对象已经构建并且你有扩展数组的功能那么imho就无法工作