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.