The MultipleIterator class
(PHP 5 >= 5.3.0)
Introduction
An Iterator that sequentially iterates over all attached iterators
Class synopsis
MultipleIterator
implements
Iterator
{
/* Constants */
/* Methods */
public
__construct
([
int
}$flags
= MultipleIterator::MIT_NEED_ALL|MultipleIterator::MIT_KEYS_NUMERIC
] )Predefined Constants
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.
Table of Contents
- 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
- 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
Коментарии
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.