- PHP 手册
- 函数参考
- 其它基本扩展
- SPL
- 迭代器
AppendIterator 类
(PHP 5 >= 5.1.0, PHP 7, PHP 8)
简介
这个迭代器能陆续遍历几个迭代器。
类摘要
class AppendIterator extends IteratorIterator { /* 方法 */ public __construct() public append(Iterator$iterator
): void
public current(): mixed
public getArrayIterator(): ArrayIterator
public getInnerIterator(): Iterator
public getIteratorIndex(): ?int
public key(): scalar
public next(): void
public rewind(): void
public valid(): bool
/* 继承的方法 */
public IteratorIterator::current(): mixed
public IteratorIterator::getInnerIterator(): ?Iterator
public IteratorIterator::key(): mixed
public IteratorIterator::next(): void
public IteratorIterator::rewind(): void
public IteratorIterator::valid(): bool
}
目录
- 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
User Contributed Notes 6 notes
up down 8 php at seanmorr dot is ¶2 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
up
down
9
joshdifabio at gmail dot com ¶6 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
up
down
0
frode at ennerd dot com ¶3 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--;
}
});
?>
up
down
-2
koambarun at evolution dot com ¶5 years ago
$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;
}
}
up
down
-4
komalbarun at gmail dot com ¶5 years ago
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;
}
}
up
down
-7
komalbarun at gmail dot com ¶5 years ago
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;
}
}
add a note
官方地址:https://www.php.net/manual/en/class.appenditerator.php