Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 关联数组的下一个迭代器方法_Php_Arrays_Iterator - Fatal编程技术网

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

Php 关联数组的下一个迭代器方法,php,arrays,iterator,Php,Arrays,Iterator,我想在PHP迭代器中使用关联数组: 可能吗 我定义了这些方法: public function rewind(){ reset($this->_arr); $this->_position = key($this->_arr); } public function current(){ return $this->_arr[$this->_position]; } public function ke

我想在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->\u位置
代码没有任何效果,因为位置是字符串(关联数组的键)

那么,如何转到这种类型数组的下一个元素呢?


为什么不从关联的
数组
创建
数组对象
?然后,您可以从此
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();
}


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

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发布了一个类似的答案,但是如果已经构建了对象,并且出于好奇,您拥有扩展数组的功能,那么这就不起作用了,为什么不使用一个呢?您能展示更多的代码吗?这将是a)你说的
classxyz实现迭代器的部分
b)你(尝试)使用它的部分。“我定义了这些方法”——它被称为“从文档按原样粘贴的副本”;-)如果这是一个持有数组的类,考虑执行<代码>迭代器聚合体< /代码>,并在<代码> GeTeTalter 中返回该数组的<代码> ArrayIterator < /代码>。如果注释和解释,该代码将更有用,所以除了OP之外的其他人可以从您的知识中受益。真的不明白为什么还要重新发明轮子。。。使用从关联数组转换成的ArrayObject获得的ArrayIterator要简单得多。。。无需写下我们自己的ArrayIterator…这只是php.net示例的一个副本我在看过php.net示例后从google来到这个页面。问题是你没有写过什么东西,却要把它记下来。我想再次提及来源,这样就可以了。评论的年代与我无关,有用的信息总是有用的。如果你不想别人评论你以前的帖子,那么,我不知道,把它删掉示例#1基本用法。对不起,如果我让你难过了。为什么你总是说2年,有那么重要吗?在
foreach
循环中,我假设这不会返回一个键,只返回一个数字表示。对吗?@OGHaza,谢谢。我觉得他最近的编辑对我来说还行吧?我的原始代码没有bug,但我想我更喜欢他的最新编辑。非常好的解决方案。还请添加计数和其他例行程序中使用的功能
$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();
}
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;
}