3v4l.org

run code in 300+ PHP versions simultaneously
<?php $data = array( array ( 'category_id' => 20, 'category_name' => 'Category 1', 'main_category' => 0, 'sub_categories' => array( array ( 'category_id' => 25, 'category_name' => 'Category 1.3', 'main_category' => 20, 'sub_categories' => [], ), array ( 'category_id' => 26, 'category_name' => 'Category 1.4', 'main_category' => 20, 'sub_categories' => array ( array ( 'category_id' => 30, 'category_name' => 'Category 1.4.1', 'main_category' => 26, 'sub_categories' => [] ), array ( 'category_id' => 31, 'category_name' => 'Category 1.4.2', 'main_category' => 26, 'sub_categories' => [] ) ) ), ) ), array ( 'category_id' => 19, 'category_name' => 'Category 2', 'main_category' => 0, 'sub_categories' => [] ) ); function getChildren($data,$parents,&$result){ foreach($data as $current_category){ $temp = []; $temp['category_id'] = $current_category['category_id']; $new_cat_sequence = array_merge($parents,[$current_category['category_name']]); $temp['category_name'] = implode(" -> ",$new_cat_sequence); $result[] = $temp; getChildren($current_category['sub_categories'],$new_cat_sequence,$result); } } $result = []; getChildren($data,[],$result); print_r($result);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/QN4NP
function name:  (null)
number of ops:  11
compiled vars:  !0 = $data, !1 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   63     1        ASSIGN                                                   !1, <array>
   64     2        INIT_FCALL                                               'getchildren'
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 <array>
          5        SEND_REF                                                 !1
          6        DO_FCALL                                      0          
   66     7        INIT_FCALL                                               'print_r'
          8        SEND_VAR                                                 !1
          9        DO_ICALL                                                 
         10      > RETURN                                                   1

Function getchildren:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 32
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 32
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 32
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 32
filename:       /in/QN4NP
function name:  getChildren
number of ops:  34
compiled vars:  !0 = $data, !1 = $parents, !2 = $result, !3 = $current_category, !4 = $temp, !5 = $new_cat_sequence
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   53     3      > FE_RESET_R                                       $6      !0, ->32
          4    > > FE_FETCH_R                                               $6, !3, ->32
   54     5    >   ASSIGN                                                   !4, <array>
   55     6        FETCH_DIM_R                                      ~9      !3, 'category_id'
          7        ASSIGN_DIM                                               !4, 'category_id'
          8        OP_DATA                                                  ~9
   56     9        INIT_FCALL                                               'array_merge'
         10        SEND_VAR                                                 !1
         11        FETCH_DIM_R                                      ~10     !3, 'category_name'
         12        INIT_ARRAY                                       ~11     ~10
         13        SEND_VAL                                                 ~11
         14        DO_ICALL                                         $12     
         15        ASSIGN                                                   !5, $12
   57    16        INIT_FCALL                                               'implode'
         17        SEND_VAL                                                 '+-%3E+'
         18        SEND_VAR                                                 !5
         19        DO_ICALL                                         $15     
         20        ASSIGN_DIM                                               !4, 'category_name'
         21        OP_DATA                                                  $15
   58    22        ASSIGN_DIM                                               !2
         23        OP_DATA                                                  !4
   59    24        INIT_FCALL_BY_NAME                                       'getChildren'
         25        CHECK_FUNC_ARG                                           
         26        FETCH_DIM_FUNC_ARG                               $17     !3, 'sub_categories'
         27        SEND_FUNC_ARG                                            $17
         28        SEND_VAR_EX                                              !5
         29        SEND_VAR_EX                                              !2
         30        DO_FCALL                                      0          
   53    31      > JMP                                                      ->4
         32    >   FE_FREE                                                  $6
   61    33      > RETURN                                                   null

End of function getchildren

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
144.76 ms | 1011 KiB | 17 Q