如果您使用 ArrayObject 的 exchangeArray 方法,然后使用 ArrayIterator 的 next 方法,如下所示
<?php
$fruits = array("apple", "grape", "lemon");
$colors = array("blue", "yellow", "green");
$arrayObject = new ArrayObject($fruits);
$arrayIterator = $arrayObject->getIterator();
while($arrayIterator->valid()) {
if ($arrayIterator->current() == "grape") {
$arrayObject->exchangeArray($colors);
}
$arrayIterator->next();
}
?>
您将收到
PHP Notice: ArrayIterator::next(): Array was modified outside object and internal position is no longer valid
所以要小心 next 和 prev 操作。:)