由於 ArrayIterator 並非真正的列表,"append" 的實作方式讓我有點困惑。使用 "append" 時,我預期新值會放在 "最後一個元素索引 + 1" 的位置。但如果您事先取消設定元素,這就不會發生。
似乎一旦索引被使用過,即使被取消設定,也會被列入黑名單,永遠不會再被使用。
如同在 https://php.dev.org.tw/manual/de/arrayiterator.offsetset.php#106775 中的建議,ArrayIterator::append 使用了帶有空索引參數的 ArrayIterator::offsetSet。因此,我用了這兩個變通方法讓 "append" 按照我想要的方式運作
<?php
class myArrayIterator extends ArrayIterator {
public function offsetSet($offset, $value) {
if (is_null($offset)) { // offset == null when it's called by ArrayIterator::append
$offset = $this->generateOffset(); // do it in a separate method
}
parent::offsetSet($offset, $value); // call the native implementation with an index
$this->ksort(); // sort it to avoid confusion when it gets dumped or iterated
}
protected function generateOffset() {
$offset = count($this); // take count as offset as it should be lastKey+1
while ($this->offsetExists($offset)) { // is it really empty?
$offset++; // try the next one until there's an empty one
}
return $offset;
}
}
class mySaveArrayIterator extends myArrayIterator {
protected function generateOffset() {
$offset = 0; // expect zero is the first possible key
while ($this->offsetExists($offset)) { // try every key until there's an empty one
$offset++;
}
return $offset;
}
}
$data = array('foo', 'bar', 'baz');
$array = new ArrayIterator($data);
$myArray = new myArrayIterator($data);
$mySaveArray = new mySaveArrayIterator($data);
// remove the last element
$array ->offsetUnset(2);
$myArray ->offsetUnset(2);
$mySaveArray->offsetUnset(2);
// append an element
$array ->append('foobar');
$myArray ->append('foobar');
$mySaveArray->append('foobar');
// check the position of the new element
print_r($array);
print_r($myArray);
print_r($mySaveArray);
// remove some element
$array ->offsetUnset(1);
$myArray ->offsetUnset(1);
$mySaveArray->offsetUnset(1);
// again append an element
$array ->append('foobarbaz');
$myArray ->append('foobarbaz');
$mySaveArray->append('foobarbaz');
// check the position of the new element
print_r($array);
print_r($myArray);
print_r($mySaveArray);
?>
輸出
ArrayIterator 物件
(
[storage:ArrayIterator:private] => 陣列
(
[0] => foo
[1] => bar
[3] => foobar
)
)
myArrayIterator 物件
(
[storage:ArrayIterator:private] => 陣列
(
[0] => foo
[1] => bar
[2] => foobar
)
)
mySaveArrayIterator 物件
(
[storage:ArrayIterator:private] => 陣列
(
[0] => foo
[1] => bar
[2] => foobar
)
)
ArrayIterator 物件
(
[storage:ArrayIterator:private] => 陣列
(
[0] => foo
[3] => foobar
[4] => foobarbaz
)
)
myArrayIterator 物件
(
[storage:ArrayIterator:private] => 陣列
(
[0] => foo
[2] => foobar
[3] => foobarbaz
)
)
mySaveArrayIterator 物件
(
[storage:ArrayIterator:private] => 陣列
(
[0] => foo
[1] => foobarbaz
[2] => foobar
)
)
這幫助我將 ArrayIterator 視為具有有效索引的連續列表。