首页  >  文章  >  后端开发  >  PHP递归遍历多维数组实现无限分类的方法

PHP递归遍历多维数组实现无限分类的方法

高洛峰
高洛峰原创
2017-01-14 11:50:071924浏览

本文实例讲述了PHP递归遍历多维数组实现无限分类的方法。分享给大家供大家参考,具体如下:

<?php
   //$data[]=array(&#39;id&#39;=>1,&#39;parentid&#39;=>0,&#39;name&#39;=>&#39;中国&#39;,&#39;img&#39;=>&#39;52091199&#39;);
   $data[]=array(&#39;id&#39;=>1,&#39;parentid&#39;=>0,&#39;name&#39;=>&#39;中国&#39;);
   $data[]=array(&#39;id&#39;=>2,&#39;parentid&#39;=>0,&#39;name&#39;=>&#39;美国&#39;);
   $data[]=array(&#39;id&#39;=>3,&#39;parentid&#39;=>0,&#39;name&#39;=>&#39;韩国&#39;);
   $data[]=array(&#39;id&#39;=>4,&#39;parentid&#39;=>1,&#39;name&#39;=>&#39;北京&#39;);
   $data[]=array(&#39;id&#39;=>5,&#39;parentid&#39;=>1,&#39;name&#39;=>&#39;上海&#39;);
   $data[]=array(&#39;id&#39;=>6,&#39;parentid&#39;=>1,&#39;name&#39;=>&#39;广西&#39;);
   $data[]=array(&#39;id&#39;=>7,&#39;parentid&#39;=>6,&#39;name&#39;=>&#39;桂林&#39;);
   $data[]=array(&#39;id&#39;=>8,&#39;parentid&#39;=>6,&#39;name&#39;=>&#39;南宁&#39;);
   $data[]=array(&#39;id&#39;=>9,&#39;parentid&#39;=>6,&#39;name&#39;=>&#39;柳州&#39;);
   $data[]=array(&#39;id&#39;=>10,&#39;parentid&#39;=>2,&#39;name&#39;=>&#39;纽约&#39;);
   $data[]=array(&#39;id&#39;=>11,&#39;parentid&#39;=>2,&#39;name&#39;=>&#39;华盛顿&#39;);
   $data[]=array(&#39;id&#39;=>12,&#39;parentid&#39;=>3,&#39;name&#39;=>&#39;首尔&#39;);
   $tree=build_tree($data,0);
   //echo memory_get_usage();
   print_r($tree);
 function findChild(&$arr,$id){
  $childs=array();
  foreach ($arr as $k => $v){
   if($v[&#39;parentid&#39;]== $id){
    $childs[]=$v;
   }
  }
  return $childs;
 }
 function build_tree($rows,$root_id){
  $childs=findChild($rows,$root_id);
  if(empty($childs)){
   return null;
  }
 foreach ($childs as $k => $v){
  $rescurTree=build_tree($rows,$v[&#39;id&#39;]);
  if( null != $rescurTree){
  $childs[$k][&#39;childs&#39;]=$rescurTree;
  }
 }
  return $childs;
 }
?>

运行结果:

Array
(
 [0] => Array
  (
   [id] => 1
   [parentid] => 0
   [name] => 中国
   [childs] => Array
    (
     [0] => Array
      (
       [id] => 4
       [parentid] => 1
       [name] => 北京
      )
     [1] => Array
      (
       [id] => 5
       [parentid] => 1
       [name] => 上海
      )
     [2] => Array
      (
       [id] => 6
       [parentid] => 1
       [name] => 广西
       [childs] => Array
        (
         [0] => Array
          (
           [id] => 7
           [parentid] => 6
           [name] => 桂林
          )
         [1] => Array
          (
           [id] => 8
           [parentid] => 6
           [name] => 南宁
          )
         [2] => Array
          (
           [id] => 9
           [parentid] => 6
           [name] => 柳州
          )
        )
      )
    )
  )
 [1] => Array
  (
   [id] => 2
   [parentid] => 0
   [name] => 美国
   [childs] => Array
    (
     [0] => Array
      (
       [id] => 10
       [parentid] => 2
       [name] => 纽约
      )
     [1] => Array
      (
       [id] => 11
       [parentid] => 2
       [name] => 华盛顿
      )
    )
  )
 [2] => Array
  (
   [id] => 3
   [parentid] => 0
   [name] => 韩国
   [childs] => Array
    (
     [0] => Array
      (
       [id] => 12
       [parentid] => 3
       [name] => 首尔
      )
    )
  )
)

希望本文所述对大家PHP程序设计有所帮助。

更多PHP递归遍历多维数组实现无限分类的方法相关文章请关注PHP中文网!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn