Multidimensional Arrays Nested to Unlimited Depth

后端 未结 6 1045
被撕碎了的回忆
被撕碎了的回忆 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:09

    Using the comments above, I've found the answer:

    function findXyz($array){
        foreach($array as $foo=>$bar){
          if (is_array($bar)){
             if ($bar["xyz"]){
                 echo "
    The array of xyz has now been found"; print_r($bar['xyz']); }else{ findXyz($bar); } } } } findXyz($myarray);

    This loops through all nested arrays and looks for any element who has a sub-array of xyz, as per my original request. array_walk_array and RecursiveIteratorIterator were unable to achieve this.

提交回复
热议问题