AppendIterator クラス
(PHP 5 >= 5.1.0, PHP 7, PHP 8)
はじめに
複数のイテレータをひとつひとつ処理するイテレータです。
クラス概要
目次
- AppendIterator::append — イテレータを追加する
- AppendIterator::__construct — AppendIterator を作成する
- AppendIterator::current — 現在の値を取得する
- AppendIterator::getArrayIterator — ArrayIterator を取得する
- AppendIterator::getIteratorIndex — イテレータのインデックスを取得する
- AppendIterator::key — 現在のキーを取得する
- AppendIterator::next — 次の要素に移動する
- AppendIterator::rewind — イテレータを巻き戻す
- AppendIterator::valid — 現在の要素が有効かどうかを調べる
+add a note
User Contributed Notes 3 notes
php at seanmorr dot is ¶
4 years ago
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
joshdifabio at gmail dot com ¶
8 years ago
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
frode at ennerd dot com ¶
6 years ago
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--;
}
});
?>
↑ and ↓ to navigate •
Enter to select •
Esc to close
Press Enter without
selection to search using Google