3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function build(){ $items = [ 1=>['categories_id'=>1,'name'=>'父1','parent_id'=>0], 9=>['categories_id'=>9,'name'=>'儿3-1','parent_id'=>3], 15=>['categories_id'=>15,'name'=>'儿3-1-1','parent_id'=>14], 3=>['categories_id'=>3,'name'=>'父3','parent_id'=>0], 4=>['categories_id'=>4,'name'=>'儿1-1','parent_id'=>1], 6=>['categories_id'=>6,'name'=>'儿1-3','parent_id'=>1], 12=>['categories_id'=>12,'name'=>'儿2-1-1','parent_id'=>7], 2=>['categories_id'=>2,'name'=>'父2','parent_id'=>0], 5=>['categories_id'=>5,'name'=>'儿1-2','parent_id'=>1], 8=>['categories_id'=>8,'name'=>'儿2-1','parent_id'=>2], 11=>['categories_id'=>11,'name'=>'儿1-1-1','parent_id'=>4], 7=>['categories_id'=>7,'name'=>'儿2-1','parent_id'=>2], 10=>['categories_id'=>10,'name'=>'儿3-1-1','parent_id'=>9], 13=>['categories_id'=>13,'name'=>'儿3-1-1','parent_id'=>10], 14=>['categories_id'=>14,'name'=>'儿3-1-1','parent_id'=>13], 16=>['categories_id'=>16,'name'=>'儿3-1-1','parent_id'=>10], ]; $tree = array(); foreach($items as $cid => $item){ if(isset($items[$item['parent_id']])){ // var_dump($tree); $items[$item['parent_id']]['childs'][$cid] = &$items[$cid]; }else{ $tree[$cid] = &$items[$cid]; } } return $tree; } $tree = build(); // print_r($tree); foreach ($tree as $cid => $item) { var_dump($item); }
based on KWQHA
Output for 5.6.30, hhvm-3.18.5 - 3.22.0, 7.0.30 - 7.3.0alpha1
array(4) { ["categories_id"]=> int(1) ["name"]=> string(4) "父1" ["parent_id"]=> int(0) ["childs"]=> array(3) { [4]=> array(4) { ["categories_id"]=> int(4) ["name"]=> string(6) "儿1-1" ["parent_id"]=> int(1) ["childs"]=> array(1) { [11]=> array(3) { ["categories_id"]=> int(11) ["name"]=> string(8) "儿1-1-1" ["parent_id"]=> int(4) } } } [6]=> array(3) { ["categories_id"]=> int(6) ["name"]=> string(6) "儿1-3" ["parent_id"]=> int(1) } [5]=> array(3) { ["categories_id"]=> int(5) ["name"]=> string(6) "儿1-2" ["parent_id"]=> int(1) } } } array(4) { ["categories_id"]=> int(3) ["name"]=> string(4) "父3" ["parent_id"]=> int(0) ["childs"]=> array(1) { [9]=> array(4) { ["categories_id"]=> int(9) ["name"]=> string(6) "儿3-1" ["parent_id"]=> int(3) ["childs"]=> array(1) { [10]=> array(4) { ["categories_id"]=> int(10) ["name"]=> string(8) "儿3-1-1" ["parent_id"]=> int(9) ["childs"]=> array(2) { [13]=> array(4) { ["categories_id"]=> int(13) ["name"]=> string(8) "儿3-1-1" ["parent_id"]=> int(10) ["childs"]=> array(1) { [14]=> array(4) { ["categories_id"]=> int(14) ["name"]=> string(8) "儿3-1-1" ["parent_id"]=> int(13) ["childs"]=> array(1) { [15]=> array(3) { ["categories_id"]=> int(15) ["name"]=> string(8) "儿3-1-1" ["parent_id"]=> int(14) } } } } } [16]=> array(3) { ["categories_id"]=> int(16) ["name"]=> string(8) "儿3-1-1" ["parent_id"]=> int(10) } } } } } } } array(4) { ["categories_id"]=> int(2) ["name"]=> string(4) "父2" ["parent_id"]=> int(0) ["childs"]=> array(2) { [8]=> array(3) { ["categories_id"]=> int(8) ["name"]=> string(6) "儿2-1" ["parent_id"]=> int(2) } [7]=> array(4) { ["categories_id"]=> int(7) ["name"]=> string(6) "儿2-1" ["parent_id"]=> int(2) ["childs"]=> array(1) { [12]=> array(3) { ["categories_id"]=> int(12) ["name"]=> string(8) "儿2-1-1" ["parent_id"]=> int(7) } } } } }