©
이 문서에서는 PHP 중국어 웹사이트 매뉴얼 풀어 주다
(PHP 5 >= 5.0.0)
ArrayIterator::append — Append an element
$value
)Appends value as the last element.
本函数还未编写文档,仅有参数列表。
value
The value to append.
没有返回值。
Note:
This method cannot be called when the ArrayIterator refers to an object .
[#1] martin dot abbrent+php dot net at ufz dot de [2013-05-02 12:49:22]
As ArrayIterator is not a real list the implementation of "append" is a little bit confusing me. When using "append" I expected the new value at the postion "last element index + 1". This will not happen when you unset elements before.
It seems like indexes once they're used are blacklisted end never used again, even if they're unset.
Like suggested in http://www.php.net/manual/de/arrayiterator.offsetset.php#106775 ArrayIterator::append uses ArrayIterator::offsetSet with empty index parameter. So I've this two workarounds to get "append" to work like I want:
<?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);
?>
Output:
ArrayIterator Object
(
[storage:ArrayIterator:private] => Array
(
[0] => foo
[1] => bar
[3] => foobar
)
)
myArrayIterator Object
(
[storage:ArrayIterator:private] => Array
(
[0] => foo
[1] => bar
[2] => foobar
)
)
mySaveArrayIterator Object
(
[storage:ArrayIterator:private] => Array
(
[0] => foo
[1] => bar
[2] => foobar
)
)
ArrayIterator Object
(
[storage:ArrayIterator:private] => Array
(
[0] => foo
[3] => foobar
[4] => foobarbaz
)
)
myArrayIterator Object
(
[storage:ArrayIterator:private] => Array
(
[0] => foo
[2] => foobar
[3] => foobarbaz
)
)
mySaveArrayIterator Object
(
[storage:ArrayIterator:private] => Array
(
[0] => foo
[1] => foobarbaz
[2] => foobar
)
)
This helped me to treat the ArrayIterator as a list with valid indexes in a serial manner.
[#2] lenye01 at gmail dot com [2011-02-10 20:11:42]
we can append values after the given array,the index is begin from 0, for example:
<?php
$b = array('name'=>'mengzhi','age'=>'22','city'=>'shanghai');
$a = new ArrayIterator($b);
$a->append(array('home'=>'china','work'=>'developer'));
var_dump($a);
?>
then output:
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" } } }