3v4l.org

run code in 300+ PHP versions simultaneously
<?php $rows = [ ['id' => 1, 'parent_id' => 0, 'name' => 'parent'], ['id' => 2, 'parent_id' => 1, 'name' => 'child'], ['id' => 3, 'parent_id' => 2, 'name' => 'grandchild'], ['id' => 4, 'parent_id' => 0, 'name' => 'parent 2'], ['id' => 5, 'parent_id' => 4, 'name' => 'child 2'], ['id' => 6, 'parent_id' => 1, 'name' => 'child'], ]; function buildTree(array $elements, $parentId = 0) { $branch = array(); foreach ($elements as $element) { if ($element['parent_id'] == $parentId) { $children = buildTree($elements, $element['id']); if ($children) { $element['children'] = $children; } $branch[] = $element; } } return $branch; } $tree = buildTree($rows); print_r($tree);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/L2jiM
function name:  (null)
number of ops:  9
compiled vars:  !0 = $rows, !1 = $tree
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   30     1        INIT_FCALL                                               'buildtree'
          2        SEND_VAR                                                 !0
          3        DO_FCALL                                      0  $3      
          4        ASSIGN                                                   !1, $3
   31     5        INIT_FCALL                                               'print_r'
          6        SEND_VAR                                                 !1
          7        DO_ICALL                                                 
          8      > RETURN                                                   1

Function buildtree:
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 = 8, Position 2 = 20
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 18
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 18
Branch analysis from position: 20
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
filename:       /in/L2jiM
function name:  buildTree
number of ops:  24
compiled vars:  !0 = $elements, !1 = $parentId, !2 = $branch, !3 = $element, !4 = $children
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      0
   13     2        ASSIGN                                                   !2, <array>
   14     3      > FE_RESET_R                                       $6      !0, ->21
          4    > > FE_FETCH_R                                               $6, !3, ->21
   15     5    >   FETCH_DIM_R                                      ~7      !3, 'parent_id'
          6        IS_EQUAL                                                 !1, ~7
          7      > JMPZ                                                     ~8, ->20
   16     8    >   INIT_FCALL_BY_NAME                                       'buildTree'
          9        SEND_VAR_EX                                              !0
         10        CHECK_FUNC_ARG                                           
         11        FETCH_DIM_FUNC_ARG                               $9      !3, 'id'
         12        SEND_FUNC_ARG                                            $9
         13        DO_FCALL                                      0  $10     
         14        ASSIGN                                                   !4, $10
   18    15      > JMPZ                                                     !4, ->18
   19    16    >   ASSIGN_DIM                                               !3, 'children'
         17        OP_DATA                                                  !4
   22    18    >   ASSIGN_DIM                                               !2
         19        OP_DATA                                                  !3
   14    20    > > JMP                                                      ->4
         21    >   FE_FREE                                                  $6
   26    22      > RETURN                                                   !2
   28    23*     > RETURN                                                   null

End of function buildtree

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
137.72 ms | 1005 KiB | 15 Q