3v4l.org

run code in 300+ PHP versions simultaneously
<?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);
Output for git.master_jit, git.master
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) } } } int(1) Warning: Trying to access array offset on value of type int in /in/ReNQQ on line 53 int(1) string(4) "父1" Fatal error: Uncaught TypeError: Cannot access offset of type string on string in /in/ReNQQ:53 Stack trace: #0 /in/ReNQQ(58): get(Array, 2) #1 /in/ReNQQ(64): get(Array, 2) #2 {main} thrown in /in/ReNQQ on line 53
Process exited with code 255.
Output for rfc.property-hooks
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) } } } int(1) Warning: Trying to access array offset on int in /in/ReNQQ on line 53 int(1) string(4) "父1" Fatal error: Uncaught TypeError: Cannot access offset of type string on string in /in/ReNQQ:53 Stack trace: #0 /in/ReNQQ(58): get(Array, 2) #1 /in/ReNQQ(64): get(Array, 2) #2 {main} thrown in /in/ReNQQ on line 53
Process exited with code 255.

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
42.46 ms | 402 KiB | 8 Q