-
-
陣列
- (
- [0] => 陣列
- (
- [id] => 1
- [name] => dev
- [parentid] => 0
- [childs] => 陣列
- (
- [0] => 陣列
- (
- [id] => 2
- [name] => php
- [parentid] => 1
- [childs] => 陣列
- (
- [0] = > 陣列
- (
- [id ] => 3
- [姓名] => smarty
- [parentid] => 2
- )
- [1] => 陣列
- (
- [id] =>; 5
- [名字] => pdo
- [parentid] =>; 2
- [孩子] =>; Array
- (
- [0] => Array
- (
- [id] => 6
- [名稱] => pdo-mysql
- [parentid] => 5
- )
)
)
)
)
[1] =>陣列
- (
- [id] => 7
- [姓名] => java
- [parentid] => 1
- )
- )
[1] =>陣列
- (
- [id] => 4
- [姓名] => life
- [parentid] => 0
- )
- )
-
複製程式碼
看有沒有更簡單的實作方法
-
-
$rows = array(
- array(
- 'id' => ; ,
- '姓名' => 'dev',
- 'parentid' => 0
- ),
- 陣列(
- 'id' => 2,
- '名稱' => 'php',
- 'parentid' => 1
- ),
- 陣列(
- 'id' => 3,
- '姓名' => '聰明',
- 'parentid ' => 2
- ),
- array(
- 'id' => 4,
- 'name' => 'life',
- 'parentid' => 0
- ),
- array(
- 'id' => 5,
- 'name' => 'pdo',
- 'parentid' => ; 2
- ),
- 陣列(
- 'id' => 6,
- '姓名' => 'pdo-mysql',
- 'parentid' => 5
- ),
- array(
- 'id' => 7,
- 'name' => 'java',
- 'parentid' => 1
- )
- ) ;
// 72648
- // 84072
- function findChild(&$arr,$id){
-
- $childs=array();
- foreach ($arr as $k => $v){
- if($v[ 'parentid']== $id){
- $childs[]=$v;
- }
- }
- 回傳$childs;
- }
function build_tree($root_id){
- 全域$rows;
- $childs=findChild($rows,$root_id);
- if(空($childs)){
- return null;
- if($childs)){
- return null;
-
- }
- foreach ($childs as $k => $v){
- $rescurTree=build_tree($v[id]);
- if( null != $rescurTree){
- $childs[ $k]['childs']=$rescurTree;
- }
- }
- return $childs;
- }
$tree=build_tree(0);
- echo memory_get_usage();
print_r($tree); ?>
複製程式碼複製程式碼您可能感興趣的文章:
php無限分類的例子(仿淘寶商品分類)
php實現的無限分類(遞歸版本)的例子
使用php數組實現的無限分類(不使用資料庫與用遞歸)
php寫的一個遞歸實現無限分類產生下拉列表的函數
|