我們知道很多開源軟體的無限分類都是採用遞歸的演算法,但是我們知道遞歸即浪費時間,又浪費空間(記憶體),
上次我也分享了個我自己原創的無限分類生成樹的方法,一熱心php高手網友給我提出了寶貴的建議,我測試了一下,這段代碼的時間非常之短,參考:
http://www.oschina.net/code/snippet_98719_11296,我再次整理了一下,發現資料庫查詢出資料之後,我們就已經定好了鍵值,因而實踐中,我們一般會在model中查詢出格式化成主鍵值對應數據的形式,因而我們可以直接用這樣的數據,就少了一層循環。程式碼也非常簡潔。
- /**
- * 此方法由@Tonton 提供
- * http://my.oschina.net/u/918697
- * @date 2012-12-12
- */
- function genTree5($items) {
- foreach ($items as $item) )
- $items[$item['pid']]['son'][$item['id']] = &$items[$item['id']];
- 回傳 isset($items[0]['son']) ?
- }
-
- /**
- * 將資料格式化成樹狀結構
- * @author Xuefen.Tong
- * @param array $items
- * @return array
- */
- function genTree9($items) {
- $tree = array(); // 建構一棵樹
- foreach ($items as $item)
- if (isset($items[$item['pid']]))
- $items[$item['pid' ] ]['son'][] = &$items[$item['id']];
- else
- $tree[] = &$items[$item['id']];
- 返回$tree;
- }
-
- $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' => '上壩村'),
- );
- echo "
- print_r(genTree5($items));
- print_r(genTree9($items));
-
- //後者輸出格式,前者類似,只是數組鍵值不一樣,不過不影響資料結構
- /*
- Array
- (
- [0] => Array
- (
- [id] => 1
- [pid] => 0
- [name] => 江西省
- [son] => Array
- (
- [0] => Array
- (
- [ id] => 3
- [pid] => 1
- [name] => 南昌市
- )
-
- [1] => Array
- (
- [id] => 13
- [pid] => 1
- [name] => 贛州市
- [son] => Array
- (
- [0] => Array
- (
- [id] => 14
- [pid] => 13
- [name] => 贛縣
- [son] => Array
- (
- [0] => Array
- (
- [id] => 16
- [pid] => 14
- [name] => 茅店鎮
- [son] => Array
- (
- [0] => Array
- (
- [id] => 18
- [pid] => 16
- [name] => 義源村
- )
-
- [1] = > Array
- (
- [id] => 19
- [pid] => 16
- [name] => 上壩村
- )
-
- )
-
- )
-
- [1] => Array
- (
- [id] => 17
- [pid] => 14
- [name] => 大田鄉
- )
-
- )
-
- )
-
- [1] => Array
- (
- [id] => 15
- [pid] => 13
- [name] => 於都縣
- )
-
- )
-
- )
-
- )
-
- )
-
- [1] => Array
- (
- [id] => 2
- [pid] => 0
- [name] => 黑龍江省
- [ son] => Array
- (
- [0] => Array
- (
- [id] => 4
- [pid] => 2
- [name] => 哈爾濱市
- [son] => Array
- (
- [0] => Array
- (
- [id] => 6
- [pid] => 4
- [name] => 香坊區
- [son] => Array
- (
- [0] => Array
- (
- [id] => 8
- [pid] => 6
- [name] => 和興路
- [son] => Array
- (
- [0] => Array
- (
- [id] => 10
- [pid ] => 8
- [name] =>
- 東北林業大學
- )
-
- [1] => Array
- (
- [id] => 12
- [pid] => 8
- [name] =>
- 哈爾濱師範大學
- )
-
- )
-
- )
-
- )
-
- )
-
- )
- )
- )
-
- [1] => Array
- (
- [id] => 7
- [pid] => 4
- [name] => 南崗區
- [ son] => Array
- (
- [0] => Array
- (
- [id] => 9
- [pid] => 7
- [name] => 西大直街
- [son] => Array
- (
- [0] => Array
- (
- [id] => 11
- [pid] => 9
- [ name] =>
- 哈爾濱工業大學
- )
-
- )
-
- )
-
- )
-
- )
-
- )
-
- )
-
- [1] => Array ( [id] => 5 [pid] => 2[name] => 雞西市
- )
-
- )
-
- )
- )*/
-
複製代碼
|