使用 ArrayObject Iterator

Php arrayiterator 允許你在迭代陣列和物件時修改和取消設定值。

例:

$array = ['1' => 'apple', '2' => 'banana', '3' => 'cherry'];

$arrayObject = new ArrayObject($array);

$iterator = $arrayObject->getIterator();

for($iterator; $iterator->valid(); $iterator->next()) {
    echo $iterator->key() . ' => ' . $iterator->current() . "</br>";
}

輸出:

1 => apple
2 => banana
3 => cherry