(PHP 5 >= 5.3.0, PHP 7, PHP 8)
MultipleIterator::key — Gets the keys of the attached iterators
Gets the current keys of all attached iterators, by calling Iterator::key() on each valid one.
With MultipleIterator::MIT_KEYS_NUMERIC, the keys are
listed in the order in which the iterators were attached. With
MultipleIterator::MIT_KEYS_ASSOC, they are keyed by
the information associated with each iterator by
MultipleIterator::attachIterator().
This function has no parameters.
An array containing the current keys of each attached
iterator. With MultipleIterator::MIT_NEED_ANY, null
is used for the iterators that are not valid.
Throws a RuntimeException if no iterator is
attached, or if MultipleIterator::MIT_NEED_ALL is set
and at least one attached iterator is not valid.
Throws an InvalidArgumentException if
MultipleIterator::MIT_KEYS_ASSOC is set and an
iterator was attached without associated information.
| Version | Description |
|---|---|
| 8.1.0 |
A RuntimeException is now thrown if
MultipleIterator::key() is called when no
iterator is attached. Previously, false was returned.
|
Example #1 MultipleIterator::key() example
<?php
$iterator = new MultipleIterator(MultipleIterator::MIT_NEED_ANY | MultipleIterator::MIT_KEYS_ASSOC);
$iterator->attachIterator(new ArrayIterator(['a' => 'apple', 'b' => 'banana']), 'fruit');
$iterator->attachIterator(new ArrayIterator(['x' => 'red']), 'color');
$iterator->rewind();
while ($iterator->valid()) {
var_dump($iterator->key());
$iterator->next();
}
?>The above example will output:
array(2) {
["fruit"]=>
string(1) "a"
["color"]=>
string(1) "x"
}
array(2) {
["fruit"]=>
string(1) "b"
["color"]=>
NULL
}