The AppendIterator class

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

简介

这个迭代器能陆续遍历几个迭代器

类摘要

AppendIterator extends IteratorIterator implements OuterIterator {
/* 方法 */
public __construct ( )
public append ( Iterator $iterator ) : void
public current ( ) : mixed
public getIteratorIndex ( ) : int|null
public key ( ) : scalar
public next ( ) : void
public rewind ( ) : void
public valid ( ) : bool
/* 继承的方法 */
public IteratorIterator::next ( ) : void
public IteratorIterator::rewind ( ) : void
public IteratorIterator::valid ( ) : bool
}

Table of Contents

User Contributed Notes

php at seanmorr dot is 25-Mar-2020 10:18
joshdifabio is technically correct, but I don't see this as a bug. You can't rewind a generator and thats what append iterator does.

If you want to use AppendIterator with Generators just wrap them with NoRewindIterator:

<?php
function foo() {
        foreach ([] as
$foo) {
                yield
$foo;
        }
}
$append = new AppendIterator();
$append->append(new NoRewindIterator(foo()));

var_dump(iterator_to_array($append));

https://3v4l.org/pgiXB
frode at ennerd dot com 06-Sep-2018 08:22
In many cases, especially for streaming sources, Generators are way more efficient. I noticed that the AppendIterator buffers the entire "inner iterator".

<?php
/**
 * This appends $next iterator to $iterator.
 */
function append_iterators(...$iterators){
    foreach(
$iterators as $iterator)
        foreach(
$iterator as $row)
            yield(
$row);
}

/**
 * Merge iterator takes one first from each iterator until
 * every iterator is empty.
 */
function merge_iterators(....$its) {
   
$numberOfIts = sizeof($its);
    while(
$numberOfIts > 0) {
       
$iterator = array_shift($its);
        yield(
$iterator->current());
       
$iterator->next();
        if(
$iterator->valid())
           
$its[] = $iterator;
        else
           
$numberOfIts--;
    }
});
?>
komalbarun at gmail dot com 16-Mar-2017 03:57
Updated code.
 I could not find how to edit a note :/

Preventing segfault if empty generator.
<?php

$append_iterator
= new \AppendIterator();

$generator = some_generator();

// Only works if first value in generator is not empty
// useful when yielding arrays
foreach ($generator as $value)
{
   
//If first $value not empty, generator is not empty.
   
if(!empty($value))
    {
       
$append_iterator->append($generator );
       
//break out of loop after appending.
       
break;
    }
}
komalbarun at gmail dot com 16-Mar-2017 03:52
Updated code.
 I could not find how to edit a note :/

Preventing segfault if empty generator.
<?php

$append_iterator
= new \AppendIterator();

$generator = ReportModel::come_generator();

// Only works if first value in generator is not empty
// useful when yielding arrays
foreach ($append_iterator as $value)
{
   
//If first $value not empty, generator is not empty.
   
if(!empty($value))
    {
       
$append_iterator->append($errors);
       
//break out of loop after appending.
   
break;
    }
}
koambarun at evolution dot com 16-Mar-2017 05:29
$append_iterator = new \AppendIterator();

$generator = ReportModel::come_generator();

foreach ($errors as $value)
{
    //If first $value not empty, generator is not empty.
    if(!empty($value))
    {
        $append_iterator->append($errors);
        //break out of loop after appending.
    break;
    }
}
joshdifabio at gmail dot com 15-Jun-2016 01:56
Note that AppendIterator will segfault when iterating over an empty generator. Do not use AppendIterator in conjunction with generators.

https://3v4l.org/YC68k

https://bugs.php.net/bug.php?id=71436
PHP8中文手册 站长在线 整理 版权归PHP文档组所有