3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ [ 'id' => 1, 'title' => 'dir 1', 'parent_id' => 0, ], [ 'id' => 2, 'title' => 'dir 2', 'parent_id' => 0, ], [ 'id' => 3, 'title' => 'dir 3', 'parent_id' => 2, ], [ 'id' => 4, 'title' => 'dir 4', 'parent_id' => 3, ], [ 'id' => 5, 'title' => 'dir 5', 'parent_id' => 6, ], [ 'id' => 6, 'title' => 'dir 6', 'parent_id' => 0, ], ]; function build_tree($array, $parent_id = 0) { $result = []; foreach ($array as $item) { if ($item['parent_id'] == $parent_id) { $new_item = $item; $new_item['children'] = build_tree($array, $new_item['id']); $result[] = $new_item; } } return $result; } $tree = build_tree($array); print_r($tree);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Fu4IY
function name:  (null)
number of ops:  9
compiled vars:  !0 = $array, !1 = $tree
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   49     1        INIT_FCALL                                               'build_tree'
          2        SEND_VAR                                                 !0
          3        DO_FCALL                                      0  $3      
          4        ASSIGN                                                   !1, $3
   51     5        INIT_FCALL                                               'print_r'
          6        SEND_VAR                                                 !1
          7        DO_ICALL                                                 
          8      > RETURN                                                   1

Function build_tree:
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/Fu4IY
function name:  build_tree
number of ops:  23
compiled vars:  !0 = $array, !1 = $parent_id, !2 = $result, !3 = $item, !4 = $new_item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      0
   38     2        ASSIGN                                                   !2, <array>
   39     3      > FE_RESET_R                                       $6      !0, ->20
          4    > > FE_FETCH_R                                               $6, !3, ->20
   40     5    >   FETCH_DIM_R                                      ~7      !3, 'parent_id'
          6        IS_EQUAL                                                 !1, ~7
          7      > JMPZ                                                     ~8, ->19
   41     8    >   ASSIGN                                                   !4, !3
   42     9        INIT_FCALL_BY_NAME                                       'build_tree'
         10        SEND_VAR_EX                                              !0
         11        CHECK_FUNC_ARG                                           
         12        FETCH_DIM_FUNC_ARG                               $11     !4, 'id'
         13        SEND_FUNC_ARG                                            $11
         14        DO_FCALL                                      0  $12     
         15        ASSIGN_DIM                                               !4, 'children'
         16        OP_DATA                                                  $12
   43    17        ASSIGN_DIM                                               !2
         18        OP_DATA                                                  !4
   39    19    > > JMP                                                      ->4
         20    >   FE_FREE                                                  $6
   46    21      > RETURN                                                   !2
   47    22*     > RETURN                                                   null

End of function build_tree

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
192.88 ms | 1398 KiB | 16 Q