3v4l.org

run code in 300+ PHP versions simultaneously
<?php $items = array( (object) array('id' => 1, 'parent_id' => 0), (object) array('id' => 2, 'parent_id' => 0), (object) array('id' => 3, 'parent_id' => 0), (object) array('id' => 4, 'parent_id' => 1), (object) array('id' => 5, 'parent_id' => 1), (object) array('id' => 6, 'parent_id' => 2), ); function buildTree($items) { $childs = array(); foreach($items as $item) $childs[$item->parent_id][] = $item; foreach($items as $item) if (isset($childs[$item->id])) $item->childs = $childs[$item->id]; return $childs[0]; } $tree = buildTree($items); print_r($tree);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/nkGDA
function name:  (null)
number of ops:  21
compiled vars:  !0 = $items, !1 = $tree
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   CAST                                          8  ~2      <array>
          1        INIT_ARRAY                                       ~3      ~2
    5     2        CAST                                          8  ~4      <array>
          3        ADD_ARRAY_ELEMENT                                ~3      ~4
    6     4        CAST                                          8  ~5      <array>
          5        ADD_ARRAY_ELEMENT                                ~3      ~5
    7     6        CAST                                          8  ~6      <array>
          7        ADD_ARRAY_ELEMENT                                ~3      ~6
    8     8        CAST                                          8  ~7      <array>
          9        ADD_ARRAY_ELEMENT                                ~3      ~7
    9    10        CAST                                          8  ~8      <array>
         11        ADD_ARRAY_ELEMENT                                ~3      ~8
    3    12        ASSIGN                                                   !0, ~3
   25    13        INIT_FCALL                                               'buildtree'
         14        SEND_VAR                                                 !0
         15        DO_FCALL                                      0  $10     
         16        ASSIGN                                                   !1, $10
   26    17        INIT_FCALL                                               'print_r'
         18        SEND_VAR                                                 !1
         19        DO_ICALL                                                 
         20      > RETURN                                                   1

Function buildtree:
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
2 jumps found. (Code = 77) Position 1 = 11, Position 2 = 20
Branch analysis from position: 11
2 jumps found. (Code = 78) Position 1 = 12, Position 2 = 20
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 19
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 19
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
Branch analysis from position: 9
filename:       /in/nkGDA
function name:  buildTree
number of ops:  24
compiled vars:  !0 = $items, !1 = $childs, !2 = $item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
   14     1        ASSIGN                                                   !1, <array>
   16     2      > FE_RESET_R                                       $4      !0, ->9
          3    > > FE_FETCH_R                                               $4, !2, ->9
   17     4    >   FETCH_OBJ_R                                      ~5      !2, 'parent_id'
          5        FETCH_DIM_W                                      $6      !1, ~5
          6        ASSIGN_DIM                                               $6
          7        OP_DATA                                                  !2
   16     8      > JMP                                                      ->3
          9    >   FE_FREE                                                  $4
   19    10      > FE_RESET_R                                       $8      !0, ->20
         11    > > FE_FETCH_R                                               $8, !2, ->20
         12    >   FETCH_OBJ_R                                      ~9      !2, 'id'
         13        ISSET_ISEMPTY_DIM_OBJ                         0          !1, ~9
         14      > JMPZ                                                     ~10, ->19
   20    15    >   FETCH_OBJ_R                                      ~12     !2, 'id'
         16        FETCH_DIM_R                                      ~13     !1, ~12
         17        ASSIGN_OBJ                                               !2, 'childs'
         18        OP_DATA                                                  ~13
   19    19    > > JMP                                                      ->11
         20    >   FE_FREE                                                  $8
   22    21        FETCH_DIM_R                                      ~14     !1, 0
         22      > RETURN                                                   ~14
   23    23*     > RETURN                                                   null

End of function buildtree

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
159.46 ms | 1403 KiB | 16 Q