如果您像這樣使用 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 提醒:ArrayIterator::next():陣列已在物件外部修改,內部位置已失效
因此,使用 next 和 prev 操作時要小心。 :)