3v4l.org

run code in 300+ PHP versions simultaneously
<?php $categories = array( array( 'parent_id' => 0, 'categories_id' => 1, ), array( 'parent_id' => 1, 'categories_id' => 11, ), array( 'parent_id' => 1, 'categories_id' => 111, ), array( 'parent_id' => 0, 'categories_id' => 2, ), array( 'parent_id' => 2, 'categories_id' => 22, ), ); $tree = array(); foreach ($categories as $category) { $tree = &build($tree, $category); } // var_export($tree); function &build(&$tree = array(), $category) { if (empty($category)) { return; } if ($category['parent_id'] == 0 && ! isset($category['childs'])) { $category = array_merge($category, array('childs' => array())); $tree = array_merge($tree, $category); return $tree; } foreach ($tree as $parent_id => $cate) { var_dump($tree); if ($category['parent_id'] == $parent_id) { $cate[$parent_id]['childs'] = $category; } if (is_array($cate)) { return build($tree, $category); } } 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/rOSrh
function name:  (null)
number of ops:  12
compiled vars:  !0 = $categories, !1 = $tree, !2 = $category
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   ASSIGN                                                   !0, <array>
   27     1        ASSIGN                                                   !1, <array>
   28     2      > FE_RESET_R                                       $5      !0, ->10
          3    > > FE_FETCH_R                                               $5, !2, ->10
   29     4    >   INIT_FCALL_BY_NAME                                       'build'
          5        SEND_VAR_EX                                              !1
          6        SEND_VAR_EX                                              !2
          7        DO_FCALL                                      0  $6      
          8        ASSIGN_REF                                               !1, $6
   28     9      > JMP                                                      ->3
         10    >   FE_FREE                                                  $5
   59    11      > RETURN                                                   1

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

End of function build

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
160.28 ms | 1400 KiB | 17 Q