The AppendIterator class
(PHP 5 >= 5.1.0)
Introduction
An Iterator that iterates over several iterators one after the other.
Class synopsis
/* Methods */
/* Inherited methods */
}Table of Contents
- AppendIterator::append — Appends an iterator
- AppendIterator::__construct — Constructs an AppendIterator
- AppendIterator::current — Gets the current value
- AppendIterator::getArrayIterator — Gets the ArrayIterator
- AppendIterator::getInnerIterator — Gets the inner iterator
- AppendIterator::getIteratorIndex — Gets an index of iterators
- AppendIterator::key — Gets the current key
- AppendIterator::next — Moves to the next element
- AppendIterator::rewind — Rewinds the Iterator
- AppendIterator::valid — Checks validity of the current element
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Другие базовые расширения
- Стандартная библиотека PHP (SPL)
- Класс AppendIterator
- Класс ArrayIterator
- Класс CachingIterator
- Класс CallbackFilterIterator
- Класс DirectoryIterator
- Класс EmptyIterator
- Класс FilesystemIterator
- Класс FilterIterator
- Класс GlobIterator
- Класс InfiniteIterator
- Класс IteratorIterator
- Класс LimitIterator
- Класс MultipleIterator
- Класс NoRewindIterator
- Класс ParentIterator
- Класс RecursiveArrayIterator
- Класс RecursiveCachingIterator
- Класс RecursiveCallbackFilterIterator
- Класс RecursiveDirectoryIterator
- Класс RecursiveFilterIterator
- Класс RecursiveIteratorIterator
- Класс RecursiveRegexIterator
- Класс RecursiveTreeIterator
- Класс RegexIterator
Коментарии
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
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--;
}
});
?>
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