我总是在 ArrayIterator 的前面丢失第二项(#1)并删除每个元素。
$cnt = 0;
$a = new ArrayIterator();
$a->append(++$cnt);
$a->append(++$cnt);
$a->append(++$cnt);
$a->append(++$cnt);
$a->append(++$cnt);
foreach ($a as $i => $item) {
print_r("$i => $item".PHP_EOL);
$a->offsetUnset($i);
}
print_r('count: '.$a->count().PHP_EOL);
foreach ($a as $i => $item) {
print_r("lost! $i => $item".PHP_EOL);
}
输出:
0 => 1
2 => 3
3 => 4
4 => 5
count: 1
lost! 1 => 2
怎么可能?oO
$ php -v
PHP 5.5.37 (cli) (built: Jun 22 2016 16:14:46)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies