伍佰目录 短网址
  当前位置:海洋目录网 » 站长资讯 » 站长资讯 » 文章详细 订阅RssFeed

PHP - Manual: AppendIterator

来源:网络转载 浏览:34445次 时间:2024-05-26
AppendIterator::append » « 迭代器
  • 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
add a note

User Contributed Notes 6 notes

up down 8 php at seanmorr dot is2 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 com6 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 com3 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 com5 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 com5 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 com5 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

  推荐站点

  • At-lib分类目录At-lib分类目录

    At-lib网站分类目录汇集全国所有高质量网站,是中国权威的中文网站分类目录,给站长提供免费网址目录提交收录和推荐最新最全的优秀网站大全是名站导航之家

    www.at-lib.cn
  • 中国链接目录中国链接目录

    中国链接目录简称链接目录,是收录优秀网站和淘宝网店的网站分类目录,为您提供优质的网址导航服务,也是网店进行收录推广,站长免费推广网站、加快百度收录、增加友情链接和网站外链的平台。

    www.cnlink.org
  • 35目录网35目录网

    35目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向35目录推荐、提交优秀网站。

    www.35mulu.com
  • 就要爱网站目录就要爱网站目录

    就要爱网站目录,按主题和类别列出网站。所有提交的网站都经过人工审查,确保质量和无垃圾邮件的结果。

    www.912219.com
  • 伍佰目录伍佰目录

    伍佰网站目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向伍佰目录推荐、提交优秀网站。

    www.wbwb.net