<?php
/*** 此方法由@tonton 提供
* * @date 2012-12-12
*/function gentree5($items)
/*** 將資料格式化成樹形結構
* @author xuefen.tong
* @param array $items
* @return array
*/function gentree9($items)
$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] => 雞西市))
))*/
PHP中不用遞迴實現無限極分類
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 ...
PHP遞迴實現無限級分類
在一些複雜的系統中,要求對資訊欄目進行無限級的分類,以增強系統的靈活性。那麼php是如何實現無限級分類的呢?我們在本文中使用遞迴演算法並結合mysql資料表實現無限級分類。在一些複雜的系統中,要求對資訊欄目進行無限級的分類,以增強系統的靈活性。那麼php是如何實現無限級分類的呢?我們在本文中使用遞迴...
PHP實現遞迴無限級分類
在一些複雜的系統中,要求對資訊欄目進行無限級的分類,以增強系統的靈活性。那麼php是如何實現無限級分類的呢?我們在本文中使用遞迴演算法並結合mysql資料表實現無限級分類。遞迴,簡單的說就是一段程式 的重複呼叫,當把 寫到乙個自定義函式中,將引數等變數儲存,函式中重複呼叫函式,直到達到某個條件才跳出...