萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> php簡單實現無限分類樹形列表的方法

php簡單實現無限分類樹形列表的方法

 這篇文章主要介紹了php簡單實現無限分類樹形列表的方法,實例分析了php通過數組實現樹形列表的技巧,具有一定參考借鑒價值,需要的朋友可以參考下

   

本文實例講述了php簡單實現無限分類樹形列表的方法。分享給大家供大家參考。具體如下:

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 $items = array( 1 => array('id' => 1, 'pid' => 0, 'name' => '江西省'), 2 => array('id' => 2, 'pid' => 0, 'name' => '黑龍江省'), 3 => array('id' => 3, 'pid' => 1, 'name' => '南昌市'), 4 => array('id' => 4, 'pid' => 2, 'name' => '哈爾濱市'), 5 => array('id' => 5, 'pid' => 2, 'name' => '雞西市'), 6 => array('id' => 6, 'pid' => 4, 'name' => '香坊區'), 7 => array('id' => 7, 'pid' => 4, 'name' => '南崗區'), 8 => array('id' => 8, 'pid' => 6, 'name' => '和興路'), 9 => array('id' => 9, 'pid' => 7, 'name' => '西大直街'), 10 => array('id' => 10, 'pid' => 8, 'name' => '東北林業大學'), 11 => array('id' => 11, 'pid' => 9, 'name' => '哈爾濱工業大學'), 12 => array('id' => 12, 'pid' => 8, 'name' => '哈爾濱師范大學'), 13 => array('id' => 13, 'pid' => 1, 'name' => '贛州市'), 14 => array('id' => 14, 'pid' => 13, 'name' => '贛縣'), 15 => array('id' => 15, 'pid' => 13, 'name' => '於都縣'), 16 => array('id' => 16, 'pid' => 14, 'name' => '茅店鎮'), 17 => array('id' => 17, 'pid' => 14, 'name' => '大田鄉'), 18 => array('id' => 18, 'pid' => 16, 'name' => '義源村'), 19 => array('id' => 19, 'pid' => 16, 'name' => '上壩村'), 20 => array('id' => 20, 'pid' => 0, 'name' => '廣東省'), 21 => array('id' => 21, 'pid' => 20, 'name' => '廣州市'), ); $t = array(); foreach ($items as $id => $item) { if ($item['pid']) { $items[$item['pid']][$item['id']] = &$items[$item['id']]; $t[] = $id; } } foreach($t as $u) { unset($items[$u]); } echo "<pre>"; print_r($items);

希望本文所述對大家的php程序設計有所幫助。

copyright © 萬盛學電腦網 all rights reserved