简体   繁体   English

PHP RecursiveIteratorIterator不会输出所有键

[英]PHP RecursiveIteratorIterator not outputting all keys

I have the following multidimensional array: 我有以下多维数组:

$array = array(
  1 => null,
  2 => array(
    3 => null,
    4 => array(
      5 => null,
    ),
    6 => array(
      7 => null,
    ),
  )
);

If I use the following code to iterate over the array 如果我使用以下代码遍历数组

$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
foreach ($iterator as $key => $value) {
  echo $key.' ';
}        

it only outputs the keys with no arrays assigned to them. 它只输出没有分配数组的键。 Ie

1 3 5 7

How can I get it to include all of the keys? 如何获得包含所有密钥的信息?

You just need to set the mode right. 您只需要正确设置模式即可。 From the manual : 手册

RecursiveIteratorIterator::SELF_FIRST - Lists leaves and parents in iteration with parents coming first. RecursiveIteratorIterator :: SELF_FIRST-列出迭代中的叶子和父对象 ,其中父对象排在第一位。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM