3v4l.org

run code in 500+ PHP versions simultaneously
<?php $rows = [ [ 'id' => '1', 'file' => 'file_1_no_subfolder.doc', 'parent_id' => 0, ], [ 'id' => '2', 'file' => 'file_2_no_subfolder.doc', 'parent_id' => 0, ], [ 'id' => '3', 'file' => 'folder-1', 'parent_id' => 0, ], [ 'id' => '4', 'file' => 'folder-1_file_1.doc', 'parent_id' => 3, ], [ 'id' => '5', 'file' => 'folder-1_file_2.doc', 'parent_id' => 3, ], [ 'id' => '6', 'file' => 'folder-1_file_3.doc', 'parent_id' => 3, ], [ 'id' => '7', 'file' => 'folder-2', 'parent_id' => 0, ], [ 'id' => 8, 'file' => 'folder-2_file-1.doc', 'parent_id' => 7, ], [ 'id' => 9, 'file' => 'folder-2_file-1.doc', 'parent_id' => 7, ], [ 'id' => 10, 'file' => 'folder-2_file-1.doc', 'parent_id' => 7, ], ]; 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/6ULBZ
function name:  (null)
number of ops:  9
compiled vars:  !0 = $rows, !1 = $tree
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                       !0, <array>
   71     1        INIT_FCALL                                                   'buildtree'
          2        SEND_VAR                                                     !0
          3        DO_FCALL                                          0  $3      
          4        ASSIGN                                                       !1, $3
   73     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/6ULBZ
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
-----------------------------------------------------------------------------------------
   55     0  E >   RECV                                                 !0      
          1        RECV_INIT                                            !1      0
   56     2        ASSIGN                                                       !2, <array>
   58     3      > FE_RESET_R                                           $6      !0, ->21
          4    > > FE_FETCH_R                                                   $6, !3, ->21
   59     5    >   FETCH_DIM_R                                          ~7      !3, 'parent_id'
          6        IS_EQUAL                                                     !1, ~7
          7      > JMPZ                                                         ~8, ->20
   60     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
   61    15      > JMPZ                                                         !4, ->18
   62    16    >   ASSIGN_DIM                                                   !3, 'children'
         17        OP_DATA                                                      !4
   64    18    >   ASSIGN_DIM                                                   !2
         19        OP_DATA                                                      !3
   58    20    > > JMP                                                          ->4
         21    >   FE_FREE                                                      $6
   68    22      > RETURN                                                       !2
   69    23*     > RETURN                                                       null

End of function buildtree

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
169.28 ms | 1641 KiB | 15 Q