php兩種無(wú)限分類方法實(shí)例
一、遞歸方法
$items = array(
array('id'=>1,'pid'=>0,'name'=>'一級(jí)11'),
array('id'=>2,'pid'=>0,'name'=>'一級(jí)12'),
array('id'=>3,'pid'=>1,'name'=>'二級(jí)21'),
array('id'=>4,'pid'=>3,'name'=>'三級(jí)31'),
array('id'=>5,'pid'=>1,'name'=>'二級(jí)22'),
array('id'=>6,'pid'=>3,'name'=>'三級(jí)32'),
array('id'=>7,'pid'=>6,'name'=>'四級(jí)41'),
);
$i = 0;
function formatTree($arr, $pid = 0){
$tree = array();
$temp = array();
global $i;
if($arr){
foreach($arr as $k=>$v){
if($v['pid'] == $pid){//
$temp = formatTree($arr, $v['id']);
$temp && $v['son'] = $temp;
$tree[] = $v;
}
}
}
return $tree;
}
print_r(formatTree($items));
二、非遞歸方法
function genTree($items) {
$tree = array(); //格式化好的樹
foreach ($items as $item)
if (isset($items[$item['pid']])){
$items[$item['pid']]['son'][] = &$items[$item['id']];
}
else{
$tree[] = &$items[$item['id']];
}
return $tree;
}
$items = array(
1 => array('id' => 1, 'pid' => 0, 'name' => '一級(jí)11'),
2 => array('id' => 2, 'pid' => 1, 'name' => '二級(jí)21'),
3 => array('id' => 3, 'pid' => 1, 'name' => '二級(jí)23'),
4 => array('id' => 4, 'pid' => 9, 'name' => '三級(jí)31'),
5 => array('id' => 5, 'pid' => 4, 'name' => '四級(jí)43'),
6 => array('id' => 6, 'pid' => 9, 'name' => '三級(jí)32'),
7 => array('id' => 7, 'pid' => 4, 'name' => '四級(jí)41'),
8 => array('id' => 8, 'pid' => 4, 'name' => '四級(jí)42'),
9 => array('id' => 9, 'pid' => 1, 'name' => '二級(jí)25'),
10 => array('id' => 10, 'pid' => 11, 'name' => '二級(jí)22'),
11 => array('id' => 11, 'pid' => 0, 'name' => '一級(jí)12'),
12 => array('id' => 12, 'pid' => 11, 'name' => '二級(jí)24'),
13 => array('id' => 13, 'pid' => 4, 'name' => '四級(jí)44'),
14 => array('id' => 14, 'pid' => 1, 'name' => '二級(jí)26'),
15 => array('id' => 15, 'pid' => 8, 'name' => '五級(jí)51'),
16 => array('id' => 16, 'pid' => 8, 'name' => '五級(jí)52'),
17 => array('id' => 17, 'pid' => 8, 'name' => '五級(jí)53'),
18 => array('id' => 18, 'pid' => 16, 'name' => '六級(jí)64'),
);
print_r(genTree($items));
相關(guān)文章
PHP指定截取字符串中的中英文或數(shù)字字符的實(shí)例分享
這篇文章主要介紹了PHP指定截取字符串中的中英文或數(shù)字字符的實(shí)例,還附帶介紹了過(guò)濾字符串中空格的方法,需要的朋友可以參考下2016-03-03
PHP計(jì)算一年多少個(gè)星期和每周的開始和結(jié)束日期
這篇文章主要介紹了PHP計(jì)算每周的開始和結(jié)束日期,php實(shí)現(xiàn)計(jì)算一年多少周,同時(shí)計(jì)算出每一周的開始日期和結(jié)束日期,需要的朋友可以參考下2014-07-07
PHP 使用位運(yùn)算實(shí)現(xiàn)四則運(yùn)算的代碼
這篇文章主要介紹了PHP 使用位運(yùn)算實(shí)現(xiàn)四則運(yùn)算的代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01
關(guān)于ThinkPhp 框架表單驗(yàn)證及ajax驗(yàn)證問(wèn)題
tp數(shù)據(jù)驗(yàn)證有兩種方式,一種是靜態(tài)方式,一種是動(dòng)態(tài)方式,下面小編給大家?guī)?lái)了ThinkPhp 框架表單驗(yàn)證及ajax驗(yàn)證問(wèn)題,感興趣的朋友一起看看吧2017-07-07
Laravel 5.2 文檔 數(shù)據(jù)庫(kù) —— 起步介紹
今天小編就為大家分享一篇Laravel 5.2 文檔 數(shù)據(jù)庫(kù) —— 起步介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
laravel5使用freetds連接sql server的方法
這篇文章主要給大家介紹了關(guān)于laravel5使用freetds連接sql server的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12
檢測(cè)codeigniter腳本消耗內(nèi)存情況的方法
這篇文章主要介紹了檢測(cè)codeigniter腳本消耗內(nèi)存情況的方法,實(shí)例分析了codeigniter中memory_usage方法及{ memory_usage}偽變量的使用技巧,需要的朋友可以參考下2015-03-03
php實(shí)現(xiàn)評(píng)論回復(fù)刪除功能
這篇文章主要為大家詳細(xì)介紹了php實(shí)現(xiàn)評(píng)論回復(fù)刪除功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05

