3v4l.org

run code in 300+ PHP versions simultaneously
<?php function buildTree(array $rows, int $parentId = 0): array { $new = []; foreach ($rows as $row) { if ($row['parent_id'] == $parentId) { $new[$row['id']] = buildTree($rows, $row['id']) ?: ''; } } return $new; } $resultSet = [ ['id' => 1, 'parent_id' => 0], ['id' => 2, 'parent_id' => 1], ['id' => 3, 'parent_id' => 2], ['id' => 4, 'parent_id' => 2], ['id' => 5, 'parent_id' => 3], ['id' => 6, 'parent_id' => 3], ['id' => 7, 'parent_id' => 4], ['id' => 8, 'parent_id' => 4], ['id' => 9, 'parent_id' => 4], ]; var_export( buildTree($resultSet) );
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ofP5r
function name:  (null)
number of ops:  8
compiled vars:  !0 = $resultSet
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   ASSIGN                                                   !0, <array>
   25     1        INIT_FCALL                                               'var_export'
   26     2        INIT_FCALL                                               'buildtree'
          3        SEND_VAR                                                 !0
          4        DO_FCALL                                      0  $2      
          5        SEND_VAR                                                 $2
   25     6        DO_ICALL                                                 
   27     7      > RETURN                                                   1

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

End of function buildtree

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
142.33 ms | 1005 KiB | 15 Q