PHP遞迴遍曆多維陣列實現無限分類的方法

來源:互聯網
上載者:User
本文執行個體講述了PHP遞迴遍曆多維陣列實現無限分類的方法。分享給大家供大家參考,具體如下:

<?php   //$data[]=array('id'=>1,'parentid'=>0,'name'=>'中國','img'=>'52091199');   $data[]=array('id'=>1,'parentid'=>0,'name'=>'中國');   $data[]=array('id'=>2,'parentid'=>0,'name'=>'美國');   $data[]=array('id'=>3,'parentid'=>0,'name'=>'韓國');   $data[]=array('id'=>4,'parentid'=>1,'name'=>'北京');   $data[]=array('id'=>5,'parentid'=>1,'name'=>'上海');   $data[]=array('id'=>6,'parentid'=>1,'name'=>'廣西');   $data[]=array('id'=>7,'parentid'=>6,'name'=>'桂林');   $data[]=array('id'=>8,'parentid'=>6,'name'=>'南寧');   $data[]=array('id'=>9,'parentid'=>6,'name'=>'柳州');   $data[]=array('id'=>10,'parentid'=>2,'name'=>'紐約');   $data[]=array('id'=>11,'parentid'=>2,'name'=>'華盛頓');   $data[]=array('id'=>12,'parentid'=>3,'name'=>'首爾');   $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['parentid']== $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['id']);  if( null != $rescurTree){  $childs[$k]['childs']=$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程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.