Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.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 ArrayIterator和current()_Php_Arrayiterator - Fatal编程技术网

PHP ArrayIterator和current()

PHP ArrayIterator和current(),php,arrayiterator,Php,Arrayiterator,我有一段PHP代码: echo 'PHP ' . phpversion()."\n"; $arr = array(); for ($i=0; $i<10; $i++) { $arr[] = new stdClass(); } $it = new ArrayIterator($arr); $i = 1; foreach ($it as $obj) { $obj->test = $i; $i++; } var_dump(current(

我有一段PHP代码:

echo 'PHP ' . phpversion()."\n";

$arr = array();
for ($i=0; $i<10; $i++)
{
    $arr[] = new stdClass();
}

$it = new ArrayIterator($arr);

$i = 1;
foreach ($it as $obj)
{
    $obj->test = $i;
    $i++;
}

var_dump(current($it));

die();
而PHP 7.4.8的输出如下:

PHP 5.3.8
object(stdClass)#805 (1) {
  ["test"]=>
  int(1)
}
PHP 7.4.8
file.php:33:boolean false

为什么它不再工作了?

看起来它在7.4.0中发生了变化,可能是一个bug。啊,它似乎在7.3中工作。好的,那么使用
current()
和使用数组的内部指针生成难以理解的代码可能是一个错误。@axiac您会反对同时使用
end()
来获取数组(可能关联)的最后一个条目吗?仅对其返回值使用
end()
是可以的。同样的结果也可以通过其他方法获得,但是
end()
更方便。它的对应项也一样,
reset()