3v4l.org

run code in 300+ PHP versions simultaneously
<?php $flat = [ ['id' => 100, 'parent_id' => 0, 'name' => 'root'], ['id' => 101, 'parent_id' => 100, 'name' => 'ch-1'], ['id' => 102, 'parent_id' => 101, 'name' => 'ch-1-1'], ['id' => 103, 'parent_id' => 101, 'name' => 'ch-1-2'], ['id' => 104, 'parent_id' => 101, 'name' => 'ch-1-2'], ['id' => 105, 'parent_id' => 104, 'name' => 'ch-1-2'], ['id' => 106, 'parent_id' => 101, 'name' => 'ch-1-2'], ['id' => 107, 'parent_id' => 101, 'name' => 'ch-1-2'], ]; $new = array(); foreach ($flat as $a){ $new[$a['parent_id']][] = $a; } $tree = createTree($new, array($flat[0])); print_r($tree); function createTree(&$list, $parent){ $tree = array(); foreach ($parent as $k=>$l){ if(isset($list[$l['id']])){ $l['children'] = createTree($list, $list[$l['id']]); } $tree[] = $l; } return $tree; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 9
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 9
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
filename:       /in/qIJrl
function name:  (null)
number of ops:  21
compiled vars:  !0 = $flat, !1 = $new, !2 = $a, !3 = $tree
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   15     1        ASSIGN                                                   !1, <array>
   16     2      > FE_RESET_R                                       $6      !0, ->9
          3    > > FE_FETCH_R                                               $6, !2, ->9
   17     4    >   FETCH_DIM_R                                      ~7      !2, 'parent_id'
          5        FETCH_DIM_W                                      $8      !1, ~7
          6        ASSIGN_DIM                                               $8
          7        OP_DATA                                                  !2
   16     8      > JMP                                                      ->3
          9    >   FE_FREE                                                  $6
   19    10        INIT_FCALL_BY_NAME                                       'createTree'
         11        SEND_VAR_EX                                              !1
         12        FETCH_DIM_R                                      ~10     !0, 0
         13        INIT_ARRAY                                       ~11     ~10
         14        SEND_VAL_EX                                              ~11
         15        DO_FCALL                                      0  $12     
         16        ASSIGN                                                   !3, $12
   20    17        INIT_FCALL                                               'print_r'
         18        SEND_VAR                                                 !3
         19        DO_ICALL                                                 
   31    20      > RETURN                                                   1

Function createtree:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 21
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 21
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 18
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 18
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
filename:       /in/qIJrl
function name:  createTree
number of ops:  24
compiled vars:  !0 = $list, !1 = $parent, !2 = $tree, !3 = $l, !4 = $k
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   23     2        ASSIGN                                                   !2, <array>
   24     3      > FE_RESET_R                                       $6      !1, ->21
          4    > > FE_FETCH_R                                       ~7      $6, !3, ->21
          5    >   ASSIGN                                                   !4, ~7
   25     6        FETCH_DIM_R                                      ~9      !3, 'id'
          7        ISSET_ISEMPTY_DIM_OBJ                         0          !0, ~9
          8      > JMPZ                                                     ~10, ->18
   26     9    >   INIT_FCALL_BY_NAME                                       'createTree'
         10        SEND_VAR_EX                                              !0
         11        CHECK_FUNC_ARG                                           
         12        FETCH_DIM_R                                      ~12     !3, 'id'
         13        FETCH_DIM_FUNC_ARG                               $13     !0, ~12
         14        SEND_FUNC_ARG                                            $13
         15        DO_FCALL                                      0  $14     
         16        ASSIGN_DIM                                               !3, 'children'
         17        OP_DATA                                                  $14
   28    18    >   ASSIGN_DIM                                               !2
         19        OP_DATA                                                  !3
   24    20      > JMP                                                      ->4
         21    >   FE_FREE                                                  $6
   30    22      > RETURN                                                   !2
   31    23*     > RETURN                                                   null

End of function createtree

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
148.25 ms | 1015 KiB | 14 Q