2024 日本 PHP 研討會

ArrayIterator::append

(PHP 5, PHP 7, PHP 8)

ArrayIterator::append附加元素

說明

public ArrayIterator::append(mixed $value): void

將值附加為最後一個元素。

警告

此函式目前沒有說明文件;僅提供其參數列表。

參數

value

要附加的值。

回傳值

不回傳任何值。

注意事項

注意:

ArrayIterator 參考一個 物件 時,無法呼叫此方法。

另請參閱

新增註釋

使用者貢獻的註釋 2 則註釋

martin dot abbrent+php dot net at ufz dot de
11 年前
由於 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 視為具有有效索引的連續列表。
lenye01 at gmail dot com
13 年前
我們可以在給定陣列後附加值,索引從 0 開始,例如

<?php
$b
= array('name'=>'mengzhi','age'=>'22','city'=>'shanghai');
$a = new ArrayIterator($b);
$a->append(array('home'=>'china','work'=>'developer'));
var_dump($a);
?>

然後輸出
object(ArrayIterator)#1 (1) { ["storage":"ArrayIterator":private]=> array(4) { ["name"]=> string(7) "mengzhi" ["age"]=> string(2) "12" ["city"]=> string(8) "shanghai" [0]=> array(2) { ["home"]=> string(5) "china" ["work"]=> string(9) "developer" } } }
To Top