PHP遞歸遍歷多維數(shù)組實現(xiàn)無限分類的方法
本文實例講述了PHP遞歸遍歷多維數(shù)組實現(xiàn)無限分類的方法。分享給大家供大家參考,具體如下:
<?php //$data[]=array('id'=>1,'parentid'=>0,'name'=>'中國','img'=>'52091199'); $data[]=array('id'=>1,'parentid'=>0,'name'=>'中國'); $data[]=array('id'=>2,'parentid'=>0,'name'=>'美國'); $data[]=array('id'=>3,'parentid'=>0,'name'=>'韓國'); $data[]=array('id'=>4,'parentid'=>1,'name'=>'北京'); $data[]=array('id'=>5,'parentid'=>1,'name'=>'上海'); $data[]=array('id'=>6,'parentid'=>1,'name'=>'廣西'); $data[]=array('id'=>7,'parentid'=>6,'name'=>'桂林'); $data[]=array('id'=>8,'parentid'=>6,'name'=>'南寧'); $data[]=array('id'=>9,'parentid'=>6,'name'=>'柳州'); $data[]=array('id'=>10,'parentid'=>2,'name'=>'紐約'); $data[]=array('id'=>11,'parentid'=>2,'name'=>'華盛頓'); $data[]=array('id'=>12,'parentid'=>3,'name'=>'首爾'); $tree=build_tree($data,0); //echo memory_get_usage(); print_r($tree); function findChild(&$arr,$id){ $childs=array(); foreach ($arr as $k => $v){ if($v['parentid']== $id){ $childs[]=$v; } } return $childs; } function build_tree($rows,$root_id){ $childs=findChild($rows,$root_id); if(empty($childs)){ return null; } foreach ($childs as $k => $v){ $rescurTree=build_tree($rows,$v['id']); if( null != $rescurTree){ $childs[$k]['childs']=$rescurTree; } } return $childs; } ?>
運行結(jié)果:
Array ( [0] => Array ( [id] => 1 [parentid] => 0 [name] => 中國 [childs] => Array ( [0] => Array ( [id] => 4 [parentid] => 1 [name] => 北京 ) [1] => Array ( [id] => 5 [parentid] => 1 [name] => 上海 ) [2] => Array ( [id] => 6 [parentid] => 1 [name] => 廣西 [childs] => Array ( [0] => Array ( [id] => 7 [parentid] => 6 [name] => 桂林 ) [1] => Array ( [id] => 8 [parentid] => 6 [name] => 南寧 ) [2] => Array ( [id] => 9 [parentid] => 6 [name] => 柳州 ) ) ) ) ) [1] => Array ( [id] => 2 [parentid] => 0 [name] => 美國 [childs] => Array ( [0] => Array ( [id] => 10 [parentid] => 2 [name] => 紐約 ) [1] => Array ( [id] => 11 [parentid] => 2 [name] => 華盛頓 ) ) ) [2] => Array ( [id] => 3 [parentid] => 0 [name] => 韓國 [childs] => Array ( [0] => Array ( [id] => 12 [parentid] => 3 [name] => 首爾 ) ) ) )
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP數(shù)組(Array)操作技巧大全》、《php排序算法總結(jié)》、《PHP常用遍歷算法與技巧總結(jié)》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計算法總結(jié)》、《PHP數(shù)學運算技巧總結(jié)》、《php正則表達式用法總結(jié)》、《PHP運算與運算符用法總結(jié)》、《php字符串(string)用法總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
php使用str_replace實現(xiàn)輸入框回車替換br的方法
這篇文章主要介紹了php使用str_replace實現(xiàn)輸入框回車替換br的方法,可實現(xiàn)使用\\n替換成br的方法,需要的朋友可以參考下2014-11-11PHP實現(xiàn)cookie跨域session共享的方法分析
這篇文章主要介紹了PHP實現(xiàn)cookie跨域session共享的方法,結(jié)合實例形式分析了php操作cookie的有效期、跨域、session存儲等相關(guān)操作技巧,需要的朋友可以參考下2019-08-08關(guān)于URL最大長度限制的相關(guān)資料查證
這篇文章主要介紹了關(guān)于URL最大長度限制的相關(guān)資料查證,這里記錄一下,方便以后使用。2014-12-12php在apache環(huán)境下實現(xiàn)gzip配置方法
這篇文章主要介紹了php在apache環(huán)境下實現(xiàn)gzip配置方法,較為詳細的分析了相關(guān)配置文件的修改技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04php setcookie函數(shù)的參數(shù)說明及其用法
這篇文章主要介紹了php setcookie函數(shù)的參數(shù)說明及其用法,需要的朋友可以參考下2014-04-04PHP連接及操作PostgreSQL數(shù)據(jù)庫的方法詳解
這篇文章主要介紹了PHP連接及操作PostgreSQL數(shù)據(jù)庫的方法,結(jié)合實例形式分析了php針對PostgreSQL數(shù)據(jù)庫的基本連接以及增刪改查等相關(guān)操作技巧,需要的朋友可以參考下2019-01-01