MultipleIterator::key
Gets the keys of the attached iterators
Description
public array MultipleIterator::key()
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.
Parameters
This function has no parameters.
Return Values
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.
Errors/Exceptions
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.
Examples
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
}
See Also
- MultipleIterator::current
- MultipleIterator::valid