MultipleIterator::key
(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().
参数
此函数没有参数。
返回值
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.
更新日志
| 版本 | 说明 |
|---|---|
| 8.1.0 |
A RuntimeException is now thrown if
MultipleIterator::key() is called when no
iterator is attached. Previously, false was returned.
|
示例
示例 #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();
}
?>以上示例会输出:
array(2) {
["fruit"]=>
string(1) "a"
["color"]=>
string(1) "x"
}
array(2) {
["fruit"]=>
string(1) "b"
["color"]=>
NULL
}
参见
- MultipleIterator::current() - Gets the current elements of the attached iterators
- MultipleIterator::valid() - Checks the validity of sub iterators
来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。
