略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: ArrayIterator::append

2024-11-13

ArrayIterator::append

(PHP 5, PHP 7, PHP 8)

ArrayIterator::appendAppend an element

说明

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

Appends value as the last element.

警告

本函数还未编写文档,仅有参数列表。

参数

value

The value to append.

返回值

没有返回值。

注释

注意:

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

参见

add a noteadd a note

User Contributed Notes 2 notes

up
3
martin dot abbrent+php dot net at ufz dot de
9 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
11 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" } } }

官方地址:https://www.php.net/manual/en/arrayiterator.append.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3