3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = [array ( 'category' => array ( 'categoryId' => 34, 'categoryName' => 'Advertising', ), 'categoryTreeNodeLevel' => 2, 'childCategoryTreeNodes' => array ( 0 => array ( 'category' => array ( 'categoryId' => 35, 'categoryName' => 'Other Advertising', ), 'categoryTreeNodeLevel' => 3, 'leafCategoryTreeNode' => 1, 'parentCategoryTreeNodeHref' => 'someurl', ), 1 => array ( 'category' => array ( 'categoryId' => 36, 'categoryName' => 'Drinks', ), 'categoryTreeNodeLevel' => 3, 'childCategoryTreeNodes' => array ( 0 => array ( 'category' => array ( 'categoryId' => 13601, 'categoryName' => 'Coca Cola', ), 'categoryTreeNodeLevel' => 4, 'leafCategoryTreeNode' => 1, 'parentCategoryTreeNodeHref' => 'someurl', ), 1 => array ( 'category' => array ( 'categoryId' => 13613, 'categoryName' => 'Pepsi', ), 'categoryTreeNodeLevel' => 4, 'leafCategoryTreeNode' => 1, 'parentCategoryTreeNodeHref' => 'someurl', ), 2 => array ( 'category' => array ( 'categoryId' => 20189, 'categoryName' => 'Other Drinks Advertising', ), 'categoryTreeNodeLevel' => 4, 'leafCategoryTreeNode' => 1, 'parentCategoryTreeNodeHref' => 'someurl', ), ), 'parentCategoryTreeNodeHref' => 'someurl', ), 2 => array ( 'category' => array ( 'categoryId' => 804, 'categoryName' => 'Signs', ), 'categoryTreeNodeLevel' => 3, 'leafCategoryTreeNode' => 1, 'parentCategoryTreeNodeHref' => 'someurl', ), 3 => array ( 'category' => array ( 'categoryId' => 821, 'categoryName' => 'Distillery/Spirits', ), 'categoryTreeNodeLevel' => 3, 'leafCategoryTreeNode' => 1, 'parentCategoryTreeNodeHref' => 'someurl', ), 4 => array ( 'category' => array ( 'categoryId' => 1313, 'categoryName' => 'Transportation', ), 'categoryTreeNodeLevel' => 3, 'childCategoryTreeNodes' => array ( 0 => array ( 'category' => array ( 'categoryId' => 69513, 'categoryName' => 'Aeronautica', ), 'categoryTreeNodeLevel' => 4, 'leafCategoryTreeNode' => 1, 'parentCategoryTreeNodeHref' => 'someurl', ), 1 => array ( 'category' => array ( 'categoryId' => 69514, 'categoryName' => 'Petrol/Oil', ), 'categoryTreeNodeLevel' => 4, 'leafCategoryTreeNode' => 1, 'parentCategoryTreeNodeHref' => 'someurl', ), 2 => array ( 'category' => array ( 'categoryId' => 69515, 'categoryName' => 'Other Transportation Advert', ), 'categoryTreeNodeLevel' => 4, 'leafCategoryTreeNode' => 1, 'parentCategoryTreeNodeHref' => 'someurl', ), 3 => array ( 'category' => array ( 'categoryId' => 90648, 'categoryName' => 'Railway', ), 'categoryTreeNodeLevel' => 4, 'leafCategoryTreeNode' => 1, 'parentCategoryTreeNodeHref' => 'someurl', ), ), 'parentCategoryTreeNodeHref' => 'someurl', ), 5 => array ( 'category' => array ( 'categoryId' => 1525, 'categoryName' => 'Chemist', ), 'categoryTreeNodeLevel' => 3, 'leafCategoryTreeNode' => 1, 'parentCategoryTreeNodeHref' => 'someurl', ), ), )]; $data = flattenCategoryNodeTree($arr); var_dump($data); function flattenCategoryNodeTree($data, $flattened = [], $categoryName = []) { foreach($data as $node) { if(array_key_exists('childCategoryTreeNodes', $node)) { $categoryName[] = $node['category']['categoryName']; $flattened = flattenCategoryNodeTree($node['childCategoryTreeNodes'], $flattened, $categoryName); continue; } else { $flattened[$node['category']['categoryId']] = implode('::', array_merge($categoryName, [$node['category']['categoryName']])); } } return $flattened; }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/2FeC5
function name:  (null)
number of ops:  9
compiled vars:  !0 = $arr, !1 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, <array>
  161     1        INIT_FCALL_BY_NAME                                       'flattenCategoryNodeTree'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0  $3      
          4        ASSIGN                                                   !1, $3
  163     5        INIT_FCALL                                               'var_dump'
          6        SEND_VAR                                                 !1
          7        DO_ICALL                                                 
  177     8      > RETURN                                                   1

Function flattencategorynodetree:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 37
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 37
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 21
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 37
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 37
filename:       /in/2FeC5
function name:  flattenCategoryNodeTree
number of ops:  40
compiled vars:  !0 = $data, !1 = $flattened, !2 = $categoryName, !3 = $node
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  165     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <array>
          2        RECV_INIT                                        !2      <array>
  167     3      > FE_RESET_R                                       $4      !0, ->37
          4    > > FE_FETCH_R                                               $4, !3, ->37
  168     5    >   ARRAY_KEY_EXISTS                                         'childCategoryTreeNodes', !3
          6      > JMPZ                                                     ~5, ->21
  169     7    >   FETCH_DIM_R                                      ~7      !3, 'category'
          8        FETCH_DIM_R                                      ~8      ~7, 'categoryName'
          9        ASSIGN_DIM                                               !2
         10        OP_DATA                                                  ~8
  170    11        INIT_FCALL_BY_NAME                                       'flattenCategoryNodeTree'
         12        CHECK_FUNC_ARG                                           
         13        FETCH_DIM_FUNC_ARG                               $9      !3, 'childCategoryTreeNodes'
         14        SEND_FUNC_ARG                                            $9
         15        SEND_VAR_EX                                              !1
         16        SEND_VAR_EX                                              !2
         17        DO_FCALL                                      0  $10     
         18        ASSIGN                                                   !1, $10
  171    19      > JMP                                                      ->4
         20*       JMP                                                      ->36
  173    21    >   FETCH_DIM_R                                      ~12     !3, 'category'
         22        FETCH_DIM_R                                      ~13     ~12, 'categoryId'
         23        INIT_FCALL                                               'implode'
         24        SEND_VAL                                                 '%3A%3A'
         25        INIT_FCALL                                               'array_merge'
         26        SEND_VAR                                                 !2
         27        FETCH_DIM_R                                      ~15     !3, 'category'
         28        FETCH_DIM_R                                      ~16     ~15, 'categoryName'
         29        INIT_ARRAY                                       ~17     ~16
         30        SEND_VAL                                                 ~17
         31        DO_ICALL                                         $18     
         32        SEND_VAR                                                 $18
         33        DO_ICALL                                         $19     
         34        ASSIGN_DIM                                               !1, ~13
         35        OP_DATA                                                  $19
  167    36      > JMP                                                      ->4
         37    >   FE_FREE                                                  $4
  176    38      > RETURN                                                   !1
  177    39*     > RETURN                                                   null

End of function flattencategorynodetree

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
189.4 ms | 1400 KiB | 19 Q