簡體   English   中英

PHP RecursiveIteratorIterator不會輸出所有鍵

[英]PHP RecursiveIteratorIterator not outputting all keys

我有以下多維數組:

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

如果我使用以下代碼遍歷數組

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

它只輸出沒有分配數組的鍵。

1 3 5 7

如何獲得包含所有密鑰的信息?

您只需要正確設置模式即可。 手冊

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