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); function get($tree, $level) { // static $tree; $lv = 1; $res = array(); foreach ($tree as $cid => $item) { /*if ($item['parent_id'] == 0) { if ($lv == $level) { continue; } $res[$cid] = $item; $lv ++; }*/ // var_dump($item); if ($item['parent_id'] == 0) { $lv = 1; } if (is_array($item)) { $lv ++; return get($item, $level); } var_dump($lv); } } $get = get($tree, 2); // var_dump($get);
based on 7c5Lq
Output for 5.6.30, 7.0.30 - 7.3.0alpha1
Array ( [1] => Array ( [categories_id] => 1 [name] => 父1 [parent_id] => 0 [childs] => Array ( [4] => Array ( [categories_id] => 4 [name] => 儿1-1 [parent_id] => 1 [childs] => Array ( [11] => Array ( [categories_id] => 11 [name] => 儿1-1-1 [parent_id] => 4 ) ) ) [6] => Array ( [categories_id] => 6 [name] => 儿1-3 [parent_id] => 1 ) [5] => Array ( [categories_id] => 5 [name] => 儿1-2 [parent_id] => 1 ) ) ) [3] => Array ( [categories_id] => 3 [name] => 父3 [parent_id] => 0 [childs] => Array ( [9] => Array ( [categories_id] => 9 [name] => 儿3-1 [parent_id] => 3 [childs] => Array ( [10] => Array ( [categories_id] => 10 [name] => 儿3-1-1 [parent_id] => 9 [childs] => Array ( [13] => Array ( [categories_id] => 13 [name] => 儿3-1-1 [parent_id] => 10 [childs] => Array ( [14] => Array ( [categories_id] => 14 [name] => 儿3-1-1 [parent_id] => 13 [childs] => Array ( [15] => Array ( [categories_id] => 15 [name] => 儿3-1-1 [parent_id] => 14 ) ) ) ) ) [16] => Array ( [categories_id] => 16 [name] => 儿3-1-1 [parent_id] => 10 ) ) ) ) ) ) ) [2] => Array ( [categories_id] => 2 [name] => 父2 [parent_id] => 0 [childs] => Array ( [8] => Array ( [categories_id] => 8 [name] => 儿2-1 [parent_id] => 2 ) [7] => Array ( [categories_id] => 7 [name] => 儿2-1 [parent_id] => 2 [childs] => Array ( [12] => Array ( [categories_id] => 12 [name] => 儿2-1-1 [parent_id] => 7 ) ) ) ) ) ) int(1) Warning: Illegal string offset 'parent_id' in /in/VKJ7C on line 53 int(1) int(1) Notice: Undefined index: parent_id in /in/VKJ7C on line 53 int(1) Warning: Illegal string offset 'parent_id' in /in/VKJ7C on line 53 int(1) int(1) Notice: Undefined index: parent_id in /in/VKJ7C on line 53 int(1) Warning: Illegal string offset 'parent_id' in /in/VKJ7C on line 53 int(1) int(1)
Output for hhvm-3.22.0
Array ( [1] => Array ( [categories_id] => 1 [name] => 父1 [parent_id] => 0 [childs] => Array ( [4] => Array ( [categories_id] => 4 [name] => 儿1-1 [parent_id] => 1 [childs] => Array ( [11] => Array ( [categories_id] => 11 [name] => 儿1-1-1 [parent_id] => 4 ) ) ) [6] => Array ( [categories_id] => 6 [name] => 儿1-3 [parent_id] => 1 ) [5] => Array ( [categories_id] => 5 [name] => 儿1-2 [parent_id] => 1 ) ) ) [3] => Array ( [categories_id] => 3 [name] => 父3 [parent_id] => 0 [childs] => Array ( [9] => Array ( [categories_id] => 9 [name] => 儿3-1 [parent_id] => 3 [childs] => Array ( [10] => Array ( [categories_id] => 10 [name] => 儿3-1-1 [parent_id] => 9 [childs] => Array ( [13] => Array ( [categories_id] => 13 [name] => 儿3-1-1 [parent_id] => 10 [childs] => Array ( [14] => Array ( [categories_id] => 14 [name] => 儿3-1-1 [parent_id] => 13 [childs] => Array ( [15] => Array ( [categories_id] => 15 [name] => 儿3-1-1 [parent_id] => 14 ) ) ) ) ) [16] => Array ( [categories_id] => 16 [name] => 儿3-1-1 [parent_id] => 10 ) ) ) ) ) ) ) [2] => Array ( [categories_id] => 2 [name] => 父2 [parent_id] => 0 [childs] => Array ( [8] => Array ( [categories_id] => 8 [name] => 儿2-1 [parent_id] => 2 ) [7] => Array ( [categories_id] => 7 [name] => 儿2-1 [parent_id] => 2 [childs] => Array ( [12] => Array ( [categories_id] => 12 [name] => 儿2-1-1 [parent_id] => 7 ) ) ) ) ) ) int(1) int(1) int(1) Notice: Undefined index: parent_id in /in/VKJ7C on line -1 int(1) int(1) int(1) Notice: Undefined index: parent_id in /in/VKJ7C on line -1 int(1) int(1) int(1)
Output for hhvm-3.18.5 - 3.21.3
Array ( [1] => Array ( [categories_id] => 1 [name] => 父1 [parent_id] => 0 [childs] => Array ( [4] => Array ( [categories_id] => 4 [name] => 儿1-1 [parent_id] => 1 [childs] => Array ( [11] => Array ( [categories_id] => 11 [name] => 儿1-1-1 [parent_id] => 4 ) ) ) [6] => Array ( [categories_id] => 6 [name] => 儿1-3 [parent_id] => 1 ) [5] => Array ( [categories_id] => 5 [name] => 儿1-2 [parent_id] => 1 ) ) ) [3] => Array ( [categories_id] => 3 [name] => 父3 [parent_id] => 0 [childs] => Array ( [9] => Array ( [categories_id] => 9 [name] => 儿3-1 [parent_id] => 3 [childs] => Array ( [10] => Array ( [categories_id] => 10 [name] => 儿3-1-1 [parent_id] => 9 [childs] => Array ( [13] => Array ( [categories_id] => 13 [name] => 儿3-1-1 [parent_id] => 10 [childs] => Array ( [14] => Array ( [categories_id] => 14 [name] => 儿3-1-1 [parent_id] => 13 [childs] => Array ( [15] => Array ( [categories_id] => 15 [name] => 儿3-1-1 [parent_id] => 14 ) ) ) ) ) [16] => Array ( [categories_id] => 16 [name] => 儿3-1-1 [parent_id] => 10 ) ) ) ) ) ) ) [2] => Array ( [categories_id] => 2 [name] => 父2 [parent_id] => 0 [childs] => Array ( [8] => Array ( [categories_id] => 8 [name] => 儿2-1 [parent_id] => 2 ) [7] => Array ( [categories_id] => 7 [name] => 儿2-1 [parent_id] => 2 [childs] => Array ( [12] => Array ( [categories_id] => 12 [name] => 儿2-1-1 [parent_id] => 7 ) ) ) ) ) ) int(1) int(1) int(1) Notice: Undefined index: parent_id in /in/VKJ7C on line 53 int(1) int(1) int(1) Notice: Undefined index: parent_id in /in/VKJ7C on line 53 int(1) int(1) int(1)