php無限極分類

來源:互聯網
上載者:User

標籤:alt   log   com   code   images   new   技術   oba   ima   

 1 $arr = array( 2     0=>array( 3         ‘cid‘=>1, 4         ‘pid‘=>0, 5         ‘name‘=>‘亞洲‘, 6     ), 7     1=>array( 8         ‘cid‘=>2, 9         ‘pid‘=>0,10         ‘name‘=>‘北美洲‘,11     ),12     2=>array(13         ‘cid‘=>3,14         ‘pid‘=>1,15         ‘name‘=>‘中國‘,16     ),17     3=>array(18         ‘cid‘=>4,19         ‘pid‘=>2,20         ‘name‘=>‘美國‘,21     ),22     4=>array(23         ‘cid‘=>5,24         ‘pid‘=>3,25         ‘name‘=>‘北京‘,26     ),27     5=>array(28         ‘cid‘=>6,29         ‘pid‘=>3,30         ‘name‘=>‘河北‘,31     ),32     6=>array(33         ‘cid‘=>7,34         ‘pid‘=>5,35         ‘name‘=>‘東城區‘,36     ),37     7=>array(38         ‘cid‘=>8,39         ‘pid‘=>5,40         ‘name‘=>‘海澱區‘,41     ),42 );

 1 private function GetTree($arr,$pid,$step){ 2     global $tree; 3     foreach($arr as $key=>$val) { 4         if($val[‘pid‘] == $pid) { 5             $flg = str_repeat(‘└―‘,$step); 6             $val[‘name‘] = $flg.$val[‘name‘]; 7             $tree[] = $val; 8             $this->GetTree($arr , $val[‘cid‘] ,$step+1); 9         }10     }11     return $tree;12 }

然後我們只需要寫一個調用的代碼就好

$newarr = $this->GetTree($arr, 0, 0);

這樣看上去是不是很簡單?

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.