PHP RecursiveArrayIterator获取当前深度

时间:2014-07-27 01:45:24

标签: php arrays

我正在使用RecursiveArrayIterator处理this array

$iterator = new RecursiveIteratorIterator(
    new RecursiveArrayIterator($array),
    RecursiveIteratorIterator::SELF_FIRST
);

foreach ($iterator as $key => $item) {
    //not working
    //echo $iterator->getDepth() . "\n";

}

在使用该类进行数组时,是否可以获得当前深度?

1 个答案:

答案 0 :(得分:0)

由于我的数据来自XML源,我能够用以下方法解决我的问题:

$sxi = new RecursiveIteratorIterator(
           new SimpleXMLIterator($xml), 
           RecursiveIteratorIterator::SELF_FIRST);

foreach ($sxi as $key => $value) {
    $currentDepth = $sxi->getDepth();
}