PHP 8.4.2 Released!

ArrayIterator::append

(PHP 5, PHP 7, PHP 8)

ArrayIterator::appendAppend an element

Description

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

Appends value as the last element.

Warning

This function is currently not documented; only its argument list is available.

Parameters

value

The value to append.

Return Values

No value is returned.

Notes

Note:

This method cannot be called when the ArrayIterator refers to an object.

See Also

add a note

User Contributed Notes 2 notes

up
2
martin dot abbrent+php dot net at ufz dot de
11 years ago
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.
up
0
lenye01 at gmail dot com
13 years ago
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" } } }
To Top