RecursiveIteratorIterator::__construct

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

RecursiveIteratorIterator::__construct构造一个 RecursiveIteratorIterator

描述

public RecursiveIteratorIterator::__construct(Traversable $iterator, int $mode = RecursiveIteratorIterator::LEAVES_ONLY, int $flags = 0)

RecursiveIterator 创建一个 RecursiveIteratorIterator

参数

iterator

正在构造的迭代器。可以是 RecursiveIteratorIteratorAggregate

mode

可选模式。可能的值是

flags

可选标志。可能的值是 RecursiveIteratorIterator::CATCH_GET_CHILD,它将忽略对 RecursiveIteratorIterator::getChildren() 的调用中抛出的异常。

示例

示例 #1 迭代 RecursiveIteratorIterator

<?php
$array
= array(
array(
array(
array(
'leaf-0-0-0-0',
'leaf-0-0-0-1'
),
'leaf-0-0-0'
),
array(
array(
'leaf-0-1-0-0',
'leaf-0-1-0-1'
),
'leaf-0-1-0'
),
'leaf-0-0'
)
);

$iterator = new RecursiveIteratorIterator(
new
RecursiveArrayIterator($array),
$mode
);
foreach (
$iterator as $key => $leaf) {
echo
"$key => $leaf", PHP_EOL;
}
?>

使用 $mode = RecursiveIteratorIterator::LEAVES_ONLY 的输出

0 => leaf-0-0-0-0
1 => leaf-0-0-0-1
0 => leaf-0-0-0
0 => leaf-0-1-0-0
1 => leaf-0-1-0-1
0 => leaf-0-1-0
0 => leaf-0-0

使用 $mode = RecursiveIteratorIterator::SELF_FIRST 的输出

0 => Array
0 => Array
0 => Array
0 => leaf-0-0-0-0
1 => leaf-0-0-0-1
1 => leaf-0-0-0
1 => Array
0 => Array
0 => leaf-0-1-0-0
1 => leaf-0-1-0-1
1 => leaf-0-1-0
2 => leaf-0-0

使用 $mode = RecursiveIteratorIterator::CHILD_FIRST 的输出

0 => leaf-0-0-0-0
1 => leaf-0-0-0-1
0 => Array
1 => leaf-0-0-0
0 => Array
0 => leaf-0-1-0-0
1 => leaf-0-1-0-1
0 => Array
1 => leaf-0-1-0
1 => Array
2 => leaf-0-0
0 => Array

添加备注

用户贡献备注 2 个备注

Tom
13 年前
注意:此构造函数的参数列表的缺失文档目前如下(根据源代码)。

参数 iter:参见 RecursiveIterator

参数 mode
- LEAVES_ONLY =(默认)仅迭代叶子
- SELF_FIRST = 在子节点之前迭代父节点
- CHILD_FIRST = 在父节点之前迭代子节点

参数 flags
- CATCH_GET_CHILD = 忽略 getChildren() 调用中的异常
Matt Creenan
14 年前
注意 LEAVES_ONLY、SELF_FIRST 和 CHILD_FIRST 无法单独使用。

它们是类常量,因此您需要使用 RecursiveIteratorIterator::LEAVES_ONLY 等。
To Top