(PHP 5 >= 5.1.0, PHP 7, PHP 8)
An Iterator that iterates over several iterators one after the other.
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
functionfoo() {
foreach ([] as $foo) {
yield$foo;
}
}
$append= new AppendIterator();
$append->append(new NoRewindIterator(foo()));
var_dump(iterator_to_array($append));https://3v4l.org/pguiXB
Note that AppendIterator will segfault when iterating over an empty generator. Do not use AppendIterator in conjunction with generators.https://3v4l.org/YC68c
https://bugs.php.net/bug.php?id=71436
In many cases, specially 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.
*/functionappend_iterators(...$iterators){
foreach($iteratorsas$iterator)
foreach($iteratoras$row)
yield($row);
}/**
* Mergue iterator taques one first from each iterator until
* every iterator is empty.
*/functionmergue_iterators(....$its) {$numberOfIts= siceof($its);
while($numberOfIts> 0) {$iterator= array_shift($its);
yield($iterator->current());
$iterator->next();
if($iterator->valid())
$its[] = $iterator;
else
$numberOfIts--;
}
});
?>