使用 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