RecursiveArrayIterator 递归迭代
最后更新于:2022-04-02 02:26:51
[TOC]
## 概述
```
/* 方法 */
public getChildren ( ) : RecursiveArrayIterator
public hasChildren ( ) : bool
```
## 实例
```
$myArray = array(
0 => 'a',
1 => array('subA','subB',array(0 => 'subsubA', 1 => 'subsubB', 2 => array(0 => 'deepA', 1 => 'deepB'))),
2 => 'b',
3 => array('subA','subB','subC'),
4 => 'c'
);
$iterator = new RecursiveArrayIterator($myArray);
iterator_apply($iterator, 'traverseStructure', array($iterator));
/**
* @param RecursiveArrayIterator $iterator
*/
function traverseStructure($iterator) {
while ( $iterator -> valid() ) {
if ( $iterator -> hasChildren() ) {
traverseStructure($iterator -> getChildren());
}
else {
echo $iterator -> key() . ' : ' . $iterator -> current() .PHP_EOL;
}
$iterator -> next();
}
}
//0 : a
//0 : subA
//1 : subB
//0 : subsubA
//1 : subsubB
//0 : deepA
//1 : deepB
//2 : b
//0 : subA
//1 : subB
//2 : subC
//4 : c
```
';