A very important thing to note about \RecursiveIteratorIterator is that it returns a flattened array when used with the iterator_to_array function. Ex:
<?php
$arr = array('Zero', 'name'=>'Adil', 'address' => array( 'city'=>'Dubai', 'tel' => array('int' => 971, 'tel'=>12345487)), '' => 'nothing');
$iterator = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($arr));
var_dump(iterator_to_array($iterator,true));
?>
This code will return :
array(6) {
[0]=>
string(4) "Zero"
["name"]=>
string(4) "Adil"
["city"]=>
string(5) "Dubai"
["int"]=>
int(91)
["tel"]=>
int(12345487)
[""]=>
string(7) "nothing"
}
To get the non-flattened proper array use the getArrayCopy() method, like so :
$iterator->getArrayCopy()
This will return
array(4) {
[0]=>
string(4) "Zero"
["name"]=>
string(4) "Adil"
["address"]=>
array(2) {
["city"]=>
string(5) "Dubai"
["tel"]=>
array(2) {
["int"]=>
int(91)
["tel"]=>
int(12345487)
}
}
[""]=>
string(7) "nothing"
}
RecursiveIteratorIterator クラス
(PHP 5)
導入
再帰的なイテレータの反復処理に使用します。
クラス概要
/* 定数 */
/* メソッド */
public __construct
( Traversable
$iterator
[, int $mode = RecursiveIteratorIterator::LEAVES_ONLY
[, int $flags = 0
]] )/* 継承したメソッド */
}定義済み定数
-
RecursiveIteratorIterator::LEAVES_ONLY -
RecursiveIteratorIterator::SELF_FIRST -
RecursiveIteratorIterator::CHILD_FIRST -
RecursiveIteratorIterator::CATCH_GET_CHILD
目次
- RecursiveIteratorIterator::beginChildren — 子を開始する
- RecursiveIteratorIterator::beginIteration — 反復処理を開始する
- RecursiveIteratorIterator::callGetChildren — 子を取得する
- RecursiveIteratorIterator::callHasChildren — 子を持つかどうかを調べる
- RecursiveIteratorIterator::__construct — RecursiveIteratorIterator を作成する
- RecursiveIteratorIterator::current — 現在の要素の値にアクセスする
- RecursiveIteratorIterator::endChildren — 子を終了する
- RecursiveIteratorIterator::endIteration — 反復処理を終了する
- RecursiveIteratorIterator::getDepth — 再帰的なイテレーションにおける現在の深さを取得する
- RecursiveIteratorIterator::getInnerIterator — 内部イテレータを取得する
- RecursiveIteratorIterator::getMaxDepth — 最大の深さを取得する
- RecursiveIteratorIterator::getSubIterator — 現在の有効なサブイテレータを取得する
- RecursiveIteratorIterator::key — 現在のキーにアクセスする
- RecursiveIteratorIterator::next — 次の要素に移動する
- RecursiveIteratorIterator::nextElement — 次の要素
- RecursiveIteratorIterator::rewind — トップレベルの内部イテレータの先頭要素にイテレータを巻き戻す
- RecursiveIteratorIterator::setMaxDepth — 最大の深さを設定する
- RecursiveIteratorIterator::valid — 現在の位置が有効かどうかをチェックする
Adil Baig @ AIdezigns
24-Jun-2011 08:17
Tom
06-Jan-2011 10:35
This class operates on a tree of elements, which is build by nesting recursive iterators into one another.
Thus you might say it is an iterator over iterators. While traversing those, the class pushes the iterators on a stack while traversing down to a leaf and removes them from the stack while going back up.
aidan at php dot net
30-Apr-2010 06:57
This example demonstrates using the getDepth() method with a RecursiveArrayIterator.
<?php
$tree = array();
$tree[1][2][3] = 'lemon';
$tree[1][4] = 'melon';
$tree[2][3] = 'orange';
$tree[2][5] = 'grape';
$tree[3] = 'pineapple';
print_r($tree);
$arrayiter = new RecursiveArrayIterator($tree);
$iteriter = new RecursiveIteratorIterator($arrayiter);
foreach ($iteriter as $key => $value) {
$d = $iteriter->getDepth();
echo "depth=$d k=$key v=$value\n";
}
?>
The output of this would be:
Array
(
[1] => Array
(
[2] => Array
(
[3] => lemon
)
[4] => melon
)
[2] => Array
(
[3] => orange
[5] => grape
)
[3] => pineapple
)
depth=2 k=3 v=lemon
depth=1 k=4 v=melon
depth=1 k=3 v=orange
depth=1 k=5 v=grape
depth=0 k=3 v=pineapple
Michiel Brandenburg
15-Jun-2009 01:40
You can use this to quickly find all the files (recursively) in a certain directory. This beats maintaining a stack yourself.
<?php
$directory = "/tmp/";
$fileSPLObjects = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($directory),
RecursiveIteratorIterator::CHILD_FIRST
);
try {
foreach( $fileSPLObjects as $fullFileName => $fileSPLObject ) {
print $fullFileName . " " . $fileSPLObject->getFilename() . "\n";
}
}
catch (UnexpectedValueException $e) {
printf("Directory [%s] contained a directory we can not recurse into", $directory);
}
?>
Note: if there is a directory contained within the directory you are searching in that you have no access to read an UnexpectedValueException will be thrown (leaving you with an empty list).
Note: objects returned are SPLFileObjects
crashrox at gmail dot com
19-Dec-2008 06:51
Recursive multidimensional array flatten using SPL
<?php
function array_flatten_recursive($array) {
if($array) {
$flat = array();
foreach(new RecursiveIteratorIterator(new RecursiveArrayIterator($array), RecursiveIteratorIterator::SELF_FIRST) as $key=>$value) {
if(!is_array($value)) {
$flat[] = $value;
}
}
return $flat;
} else {
return false;
}
}
$array = array(
'A' => array('B' => array( 1, 2, 3, 4, 5)),
'C' => array( 6,7,8,9)
);
print_r(array_flatten_recursive($array));
?>
-- Returns:
Array (
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
)
