Multidimensional Arrays Nested to Unlimited Depth

后端 未结 6 1035
被撕碎了的回忆
被撕碎了的回忆 2020-12-06 07:33

I have a multidimensional array nested to an unknown/unlimited depth. I\'d like to be able to loop through every element. I don\'t want to use, foreach(){foreach(){for

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-06 08:10

    There is a vast difference between unknown and unlimited. However, you can make use of the SPL Iterators instead of using multiple nested foreach loops.

    Example:

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

提交回复
热议问题