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

PHP - Manual: MultipleIterator

来源:网络转载 浏览:44796次 时间:2023-11-07
MultipleIterator::attachIterator » « LimitIterator::valid
  • PHP 手册
  • 函数参考
  • 其它基本扩展
  • SPL
  • 迭代器

The MultipleIterator class

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

简介

An Iterator that sequentially iterates over all attached iterators

类摘要

class MultipleIterator implements Iterator { /* 常量 */ const int MIT_NEED_ANY = 0; const int MIT_NEED_ALL = 1; const int MIT_KEYS_NUMERIC = 0; const int MIT_KEYS_ASSOC = 2; /* 方法 */ public __construct(int $flags = MultipleIterator::MIT_NEED_ALL | MultipleIterator::MIT_KEYS_NUMERIC) public attachIterator(Iterator $iterator, string|int|null $info = null): void public containsIterator(Iterator $iterator): bool public countIterators(): int public current(): array public detachIterator(Iterator $iterator): void public getFlags(): int public key(): array public next(): void public rewind(): void public setFlags(int $flags): void public valid(): bool }

预定义常量

MultipleIterator::MIT_NEED_ANY

Do not require all sub iterators to be valid in iteration.

MultipleIterator::MIT_NEED_ALL

Require all sub iterators to be valid in iteration.

MultipleIterator::MIT_KEYS_NUMERIC

Keys are created from the sub iterators position.

MultipleIterator::MIT_KEYS_ASSOC

Keys are created from sub iterators associated information.

目录

  • MultipleIterator::attachIterator — Attaches iterator information
  • MultipleIterator::__construct — Constructs a new MultipleIterator
  • MultipleIterator::containsIterator — Checks if an iterator is attached
  • MultipleIterator::countIterators — Gets the number of attached iterator instances
  • MultipleIterator::current — Gets the registered iterator instances
  • MultipleIterator::detachIterator — Detaches an iterator
  • MultipleIterator::getFlags — Gets the flag information
  • MultipleIterator::key — Gets the registered iterator instances
  • MultipleIterator::next — Moves all attached iterator instances forward
  • MultipleIterator::rewind — Rewinds all attached iterator instances
  • MultipleIterator::setFlags — Sets flags
  • MultipleIterator::valid — Checks the validity of sub iterators
add a note

User Contributed Notes 1 note

up down 29 sven at rtbg dot de7 years ago This iterator has a misleading name and description - it actually acts as a parallel iterator: You attach one or more iterators with a key, integer or NULL, and when you iterate over the MultipleIterator, as the result for current() you get ALL results from all attached iterators as an array (under the key or integer you attached it with), and the same is true for the key() call.

valid() will be valid if any or all iterators are valid, depending on the setting of the $flags - with ANY, you can iterate over a set of iterators with some of them ending before others, and get NULL results from these iterators until the last iterator is at it's end. With ALL, iteration stops when the first iterator stops delivering results.

next() and rewind() will be called on all attached iterators in every case.

<?php

$it1 = new ArrayIterator(array(1,2,3));
$it2 = new ArrayIterator(array(4,5,6));

$multipleIterator = new MultipleIterator(MultipleIterator::MIT_NEED_ALL|MultipleIterator::MIT_KEYS_ASSOC);

$multipleIterator->attachIterator($it1, 1);
$multipleIterator->attachIterator($it2, 'second');

foreach ($multipleIterator as $key => $value) {
    echo "Key\n"; var_dump($key);
    echo "Value\n"; var_dump($value);
    echo "---next---\n";
}
?>

Result with PHP 5.5.0 and up:

Key
array(2) {
  [1]=>
  int(0)
  ["second"]=>
  int(0)
}
Value
array(2) {
  [1]=>
  int(1)
  ["second"]=>
  int(4)
}
---next---
Key
array(2) {
  [1]=>
  int(1)
  ["second"]=>
  int(1)
}
Value
array(2) {
  [1]=>
  int(2)
  ["second"]=>
  int(5)
}
---next---
Key
array(2) {
  [1]=>
  int(2)
  ["second"]=>
  int(2)
}
Value
array(2) {
  [1]=>
  int(3)
  ["second"]=>
  int(6)
}
---next---

Note that PHP 5.4 and 5.3 do not support accessing the key() values in foreach loops because they expect them to not be an array - doing so will cause "Warning: Illegal type returned from MultipleIterator::key()" and the result of (int)0 as the key for all iterations.

Without the MultipleIterator::MIT_KEYS_ASSOC flag, the MultipleIterator will create numeric indices based on the order of attachment.
add a note

官方地址:https://www.php.net/manual/en/class.multipleiterator.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