嵌套到无限深度的多维数组 [英] Multidimensional Arrays Nested to Unlimited Depth

查看:93
本文介绍了嵌套到无限深度的多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌套到未知/无限深度的多维数组. 我希望能够遍历每个元素. 我不想使用foreach(){foreach(){foreach(){}}},因为我不知道深度.

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(){foreach(){}}} as I don't know the depth.

我最终正在寻找所有名为"xyz"的嵌套数组.有人有任何建议吗?

I'm eventually looking for all nested arrays called "xyz". Has anyone got any suggestions?

推荐答案

使用上面的注释,我找到了答案:

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 "<br />The array of xyz has now been found";
             print_r($bar['xyz']);
         }else{
            findXyz($bar);   
         } 
      }
    } 
}
findXyz($myarray);

这将遍历所有嵌套数组,并根据我的原始请求查找具有xyz子数组的任何元素. array_walk_array和RecursiveIteratorIterator无法实现这一目标.

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.

这篇关于嵌套到无限深度的多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆