3v4l.org

run code in 300+ PHP versions simultaneously
<?php $data = [ 0 => [ "category_id" => 20, "category_name" => "Category 1" , "sub_categories" => [ 0 => [ "category_id" => 25, "category_name" => "Category 1.1", "sub_categories" => '' ], 1=> [ "category_id" => 26, "category_name" => "Category 1.2", "sub_categories" => '' ], 2 => [ "category_id" => 27, "category_name" => "Category 1.3", "sub_categories" => [ 0 => [ "category_id" => 31, "category_name" => "Category 2.1", "sub_categories" => '' ], 1=> [ "category_id" => 32, "category_name" => "Category 2.2", "sub_categories" => '' ], ] ], ] ], 1=> [ "category_id" => 21, "category_name" => "Category 2", "sub_categories" => '' ], ]; function getCats($data, array &$ar, $res = ''){ foreach($data as $arr){ $res_new = ''; $res_new .= $arr['category_name'] && $res ? $res.' -> '.$arr['category_name'] : $arr['category_name']; $ar[] = ["category_id"=>$arr['category_id'], "category_name"=>$res_new, ]; // echo $res_new.PHP_EOL; // echo .PHP_EOL; if(is_array($arr['sub_categories'])){ if (count($arr['sub_categories'])>0){ getCats($arr['sub_categories'], $ar, $res_new); } } } } $ares = []; getCats($data,$ares); print_r($ares);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/gRnZQ
function name:  (null)
number of ops:  10
compiled vars:  !0 = $data, !1 = $ares
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, <array>
   62     1        ASSIGN                                                   !1, <array>
   63     2        INIT_FCALL                                               'getcats'
          3        SEND_VAR                                                 !0
          4        SEND_REF                                                 !1
          5        DO_FCALL                                      0          
   64     6        INIT_FCALL                                               'print_r'
          7        SEND_VAR                                                 !1
          8        DO_ICALL                                                 
          9      > RETURN                                                   1

Function getcats:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 38
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 38
Branch analysis from position: 5
2 jumps found. (Code = 46) Position 1 = 8, Position 2 = 9
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 15
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 26, Position 2 = 37
Branch analysis from position: 26
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 37
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 37
Branch analysis from position: 37
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 26, Position 2 = 37
Branch analysis from position: 26
Branch analysis from position: 37
Branch analysis from position: 9
Branch analysis from position: 38
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 38
filename:       /in/gRnZQ
function name:  getCats
number of ops:  40
compiled vars:  !0 = $data, !1 = $ar, !2 = $res, !3 = $arr, !4 = $res_new
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      ''
   46     3      > FE_RESET_R                                       $5      !0, ->38
          4    > > FE_FETCH_R                                               $5, !3, ->38
   48     5    >   ASSIGN                                                   !4, ''
   49     6        FETCH_DIM_R                                      ~7      !3, 'category_name'
          7      > JMPZ_EX                                          ~7      ~7, ->9
          8    >   BOOL                                             ~7      !2
          9    > > JMPZ                                                     ~7, ->15
         10    >   CONCAT                                           ~8      !2, '+-%3E+'
         11        FETCH_DIM_R                                      ~9      !3, 'category_name'
         12        CONCAT                                           ~10     ~8, ~9
         13        QM_ASSIGN                                        ~11     ~10
         14      > JMP                                                      ->17
         15    >   FETCH_DIM_R                                      ~12     !3, 'category_name'
         16        QM_ASSIGN                                        ~11     ~12
         17    >   ASSIGN_OP                                     8          !4, ~11
   51    18        FETCH_DIM_R                                      ~15     !3, 'category_id'
         19        INIT_ARRAY                                       ~16     ~15, 'category_id'
         20        ADD_ARRAY_ELEMENT                                ~16     !4, 'category_name'
         21        ASSIGN_DIM                                               !1
         22        OP_DATA                                                  ~16
   55    23        FETCH_DIM_R                                      ~17     !3, 'sub_categories'
         24        TYPE_CHECK                                  128          ~17
         25      > JMPZ                                                     ~18, ->37
   56    26    >   FETCH_DIM_R                                      ~19     !3, 'sub_categories'
         27        COUNT                                            ~20     ~19
         28        IS_SMALLER                                               0, ~20
         29      > JMPZ                                                     ~21, ->37
   57    30    >   INIT_FCALL_BY_NAME                                       'getCats'
         31        CHECK_FUNC_ARG                                           
         32        FETCH_DIM_FUNC_ARG                               $22     !3, 'sub_categories'
         33        SEND_FUNC_ARG                                            $22
         34        SEND_VAR_EX                                              !1
         35        SEND_VAR_EX                                              !4
         36        DO_FCALL                                      0          
   46    37    > > JMP                                                      ->4
         38    >   FE_FREE                                                  $5
   61    39      > RETURN                                                   null

End of function getcats

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
138.79 ms | 1017 KiB | 15 Q