3v4l.org

run code in 300+ PHP versions simultaneously
<?php $categories = array( array( 'parent_id' => 0, 'categories_id' => 1, ), array( 'parent_id' => 0, 'categories_id' => 2, ), array( 'parent_id' => 1, 'categories_id' => 11, ), array( 'parent_id' => 1, 'categories_id' => 111, ), array( 'parent_id' => 2, 'categories_id' => 22, ), ); $tree = array(); foreach ($categories as $category) { $tree = &build($category, $tree); } var_export($tree); function &build($category, &$tree = array()) { if ($category['parent_id'] == 0) { $category = array( $category['categories_id'] => $category, ); $tree = array_merge($tree, $category); return $tree; } foreach ($tree as $cate_id => $cate) { if ($category['parent_id'] == $cate_id) { $tree[$cate_id]['childs'] = $category; } if (is_array($cate)) { return build($cate, $tree); } } return $tree; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 10
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 10
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
filename:       /in/QdVCu
function name:  (null)
number of ops:  15
compiled vars:  !0 = $categories, !1 = $tree, !2 = $category
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   26     1        ASSIGN                                                   !1, <array>
   27     2      > FE_RESET_R                                       $5      !0, ->10
          3    > > FE_FETCH_R                                               $5, !2, ->10
   28     4    >   INIT_FCALL_BY_NAME                                       'build'
          5        SEND_VAR_EX                                              !2
          6        SEND_VAR_EX                                              !1
          7        DO_FCALL                                      0  $6      
          8        ASSIGN_REF                                               !1, $6
   27     9      > JMP                                                      ->3
         10    >   FE_FREE                                                  $5
   31    11        INIT_FCALL                                               'var_export'
         12        SEND_VAR                                                 !1
         13        DO_ICALL                                                 
   55    14      > RETURN                                                   1

Function build:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 14
Branch analysis from position: 5
Return found
Branch analysis from position: 14
2 jumps found. (Code = 77) Position 1 = 15, Position 2 = 32
Branch analysis from position: 15
2 jumps found. (Code = 78) Position 1 = 16, Position 2 = 32
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 23
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 31
Branch analysis from position: 25
Return found
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
Branch analysis from position: 23
Branch analysis from position: 32
Return found
Branch analysis from position: 32
filename:       /in/QdVCu
function name:  build
number of ops:  35
compiled vars:  !0 = $category, !1 = $tree, !2 = $cate, !3 = $cate_id
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <array>
   34     2        FETCH_DIM_R                                      ~4      !0, 'parent_id'
          3        IS_EQUAL                                                 ~4, 0
          4      > JMPZ                                                     ~5, ->14
   36     5    >   FETCH_DIM_R                                      ~6      !0, 'categories_id'
          6        INIT_ARRAY                                       ~7      !0, ~6
   35     7        ASSIGN                                                   !0, ~7
   39     8        INIT_FCALL                                               'array_merge'
          9        SEND_VAR                                                 !1
         10        SEND_VAR                                                 !0
         11        DO_ICALL                                         $9      
         12        ASSIGN                                                   !1, $9
   41    13      > RETURN_BY_REF                                            !1
   44    14    > > FE_RESET_R                                       $11     !1, ->32
         15    > > FE_FETCH_R                                       ~12     $11, !2, ->32
         16    >   ASSIGN                                                   !3, ~12
   45    17        FETCH_DIM_R                                      ~14     !0, 'parent_id'
         18        IS_EQUAL                                                 !3, ~14
         19      > JMPZ                                                     ~15, ->23
   46    20    >   FETCH_DIM_W                                      $16     !1, !3
         21        ASSIGN_DIM                                               $16, 'childs'
         22        OP_DATA                                                  !0
   49    23    >   TYPE_CHECK                                  128          !2
         24      > JMPZ                                                     ~18, ->31
   50    25    >   INIT_FCALL_BY_NAME                                       'build'
         26        SEND_VAR_EX                                              !2
         27        SEND_VAR_EX                                              !1
         28        DO_FCALL                                      0  $19     
         29        FE_FREE                                                  $11
         30      > RETURN_BY_REF                                            $19
   44    31    > > JMP                                                      ->15
         32    >   FE_FREE                                                  $11
   54    33      > RETURN_BY_REF                                            !1
   55    34*     > RETURN_BY_REF                                            null

End of function build

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
181.08 ms | 945 KiB | 18 Q