現在的分類資料庫設計基本都是:每乙個分類有乙個id主鍵字段,乙個pid指向父類的id,這樣便可實現無限級分類,取出的資料就是如下的格式:
$arr = array(
array("id" => 1 , "pid" => 0 , 'cat' => '欄目一'),
array("id" => 2 , "pid" => 0 , 'cat' => '欄目二'),
array("id" => 3 , "pid" => 1 , 'cat' => '欄目三'),
array("id" => 4 , "pid" => 2 , 'cat' => '欄目四'),
array("id" => 5 , "pid" => 1 , 'cat' => '欄目五'),
array("id" =>cpvjzbe; 6 , "pid" => 5 , 'cat' => '欄目六'),
array("id" => 7 , "pid" => 5 , 'cat' => '欄目七'),
array("id" => 8 , "pid" => 6 , 'cat' => '欄目八'),
array("id" => 9 , "pid" => 1 , 'cat' => '欄目九'),
array("id" => 10 , "pid" => 0 , 'cat' => '欄目十'),
array("id" => 11 , "pid" => 10 , 'cat' => '欄目十一'),
array("id" => 12 , "pid" => 11 , 'cat' => '欄目十二'),
array("id" => 13 , "pid" => 2 , 'cat' => '欄目十三'),
array("id" => 14, "pid" => 13 , 'cat' => '欄目十四')
);不多說,直接上處理**:
//生成無限極分類樹
function make_tree($arr)
foreach($arr as $k => $v)else
}} return $tree;
}測試執行:
$cat = make_tree($arr);
print_r($cat);
執行結果:
array
( [0] => array
([id] => 1
[pid] => 0
[cat] => 欄目一
[subcat] => array
([0] => array
([id] => 3
[pid] => 1
[cat] => 欄目三
)[1] => array
([id] => 5
[pid] => 1
[cat] => 欄目五
[subcat] => array
([0] => array
([id] => 6
[pid] => 5
[cat] => 欄目六
[subcat] => array
([0] => array
www.cppcns.com (
[id] => 8
[pid] => 6
[cat] => 欄目八
)))
[1] => array
([id] => 7
[pid] => 5
[cat] => 欄目七))
)[2] => array
([id] => 9
[pid] => 1
[cat] => 欄目九
)cpvjzbe))
[1] => array
([id] => 2
[pid] => 0
[cat] => 欄目二
[subcat] => array
([0] => array
([id] => 4
[pid] => 2
[cat] => 欄目四
)[1] => array
([id] => 13
[pid] => 2
[cat] => 欄目十三
[subcat] => array
([0] => array
([id] => 14
[pid] => 13
[cat] => 欄目十四))
))
)[2] => array
([id] => 10
[pid] => 0
[cat] => 欄目十
[subcat] => array
([0] => array
([id] => 11
[pid] => 10
[cat] => 欄目十一
[subcat] => array
(www.cppcns.com [0] => array
([id] => 12
[pid] => 11
[cat] => 欄目十二))
))))
如果大家需要這樣的組裝格式,或者需要該格式方便後續的處理,可以嘗試此方法
PHP實現無限極分類生成分類樹的方法
現在的分類資料庫設計基本都是 每乙個分類有乙個id主鍵字段,乙個pid指向父類的id,這樣便可實現無限級分類,取出的資料就是如下的格式 arr array array id 1 pid 0 cat 欄目一 array id 2 pid 0 cat 欄目二 array id 3 pid 1 cat 欄...
PHP無限極分類樹
建表資料 create table limit tree id int 10 unsigned not null auto increment,pid int 10 unsigned not null default 0 name varchar 64 not null default primar...
php實現無限極分類
利用兩個for迴圈實現無限級分類 表 欄位名字段型別 備註預設值 idint 主鍵 auto increment name varchar 分類名稱 pidint 父類id0 頂級分類的 pid 預設就是0了。當我們想取出某個分類的子分類樹的時候,基本思路就是遞迴,當然,出於效率問題不建議每次遞迴都...