3v4l.org

run code in 300+ PHP versions simultaneously
<?php $itemList = array( array("id" => 100,"categorypath" => "level1/powders", "product" => "Mens powder"), array("id" => 200,"categorypath" => "level1/oils/sunflower", "product" => "XYZ oil"), array("id" => 300,"categorypath" => "level1/eatable/vegetables", "product" => "carrot"), array("id" => 400,"categorypath" => "level1/oils/sunflower", "product" => "ABC oil"), array("id" => 500,"categorypath" => "level1/eatable/fruits", "product" => "mango"), array("id" => 600,"categorypath" => "level1/eatable/vegetables", "product" => "spinach"), array("id" => 700,"categorypath" => "level2/baby items/toys", "product" => "puzzle block"), array("id" => 800,"categorypath" => "level2/baby items/toys", "product" => "trucks and cars"), array("id" => 900,"categorypath" => "level2/baby items/clothes", "product" => "shirts"), array("id" => 1000,"categorypath" => "level1/powders", "product" => "Womens powder"), array("id" => 1100,"categorypath" => "level1/oils/groundnut", "product" => "GN oil"), ); // First, group products into their categories. $categoryList = []; foreach ($itemList as $item){ $categoryList[$item['categorypath']][] = [ 'id' => $item['id'] , 'path' => $item['categorypath'] , 'prodname' => $item['product'] ]; } // Now, create a tree structure out of the categories $jsonTree = ['category'=>[]]; $lookupTable = []; foreach ($categoryList as $category=>$productList){ $componentList = explode('/', $category); $path = ''; $parent = &$jsonTree; do { $component = array_shift($componentList); $path .= $component.'/'; $entry = &$lookupTable[$path]; if (!$entry){ $entry = [ 'categoryName' => $component ]; $parent['category'][] = &$entry; } $parent = &$entry; } while ($componentList); $entry['products'] = $productList; } echo json_encode($jsonTree, JSON_PRETTY_PRINT);

Abusive script

This script was stopped while abusing our resources

Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 15
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 15
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 15
2 jumps found. (Code = 77) Position 1 = 19, Position 2 = 48
Branch analysis from position: 19
2 jumps found. (Code = 78) Position 1 = 20, Position 2 = 48
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 38, Position 2 = 43
Branch analysis from position: 38
2 jumps found. (Code = 44) Position 1 = 45, Position 2 = 28
Branch analysis from position: 45
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
Branch analysis from position: 28
Branch analysis from position: 43
Branch analysis from position: 48
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 48
Branch analysis from position: 15
filename:       /in/fTUWr
function name:  (null)
number of ops:  55
compiled vars:  !0 = $itemList, !1 = $categoryList, !2 = $item, !3 = $jsonTree, !4 = $lookupTable, !5 = $productList, !6 = $category, !7 = $componentList, !8 = $path, !9 = $parent, !10 = $component, !11 = $entry
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   19     1        ASSIGN                                                   !1, <array>
   20     2      > FE_RESET_R                                       $14     !0, ->15
          3    > > FE_FETCH_R                                               $14, !2, ->15
   21     4    >   FETCH_DIM_R                                      ~15     !2, 'categorypath'
   22     5        FETCH_DIM_R                                      ~18     !2, 'id'
          6        INIT_ARRAY                                       ~19     ~18, 'id'
   23     7        FETCH_DIM_R                                      ~20     !2, 'categorypath'
          8        ADD_ARRAY_ELEMENT                                ~19     ~20, 'path'
   24     9        FETCH_DIM_R                                      ~21     !2, 'product'
         10        ADD_ARRAY_ELEMENT                                ~19     ~21, 'prodname'
   21    11        FETCH_DIM_W                                      $16     !1, ~15
         12        ASSIGN_DIM                                               $16
   24    13        OP_DATA                                                  ~19
   20    14      > JMP                                                      ->3
         15    >   FE_FREE                                                  $14
   29    16        ASSIGN                                                   !3, <array>
   30    17        ASSIGN                                                   !4, <array>
   31    18      > FE_RESET_R                                       $24     !1, ->48
         19    > > FE_FETCH_R                                       ~25     $24, !5, ->48
         20    >   ASSIGN                                                   !6, ~25
   32    21        INIT_FCALL                                               'explode'
         22        SEND_VAL                                                 '%2F'
         23        SEND_VAR                                                 !6
         24        DO_ICALL                                         $27     
         25        ASSIGN                                                   !7, $27
   34    26        ASSIGN                                                   !8, ''
   35    27        ASSIGN_REF                                               !9, !3
   37    28    >   INIT_FCALL                                               'array_shift'
         29        SEND_REF                                                 !7
         30        DO_ICALL                                         $31     
         31        ASSIGN                                                   !10, $31
   38    32        CONCAT                                           ~33     !10, '%2F'
         33        ASSIGN_OP                                     8          !8, ~33
   40    34        FETCH_DIM_W                                      $35     !4, !8
         35        ASSIGN_REF                                               !11, $35
   41    36        BOOL_NOT                                         ~37     !11
         37      > JMPZ                                                     ~37, ->43
   43    38    >   INIT_ARRAY                                       ~38     !10, 'categoryName'
   42    39        ASSIGN                                                   !11, ~38
   46    40        FETCH_DIM_W                                      $40     !9, 'category'
         41        FETCH_DIM_W                                      $41     $40
         42        ASSIGN_REF                                               $41, !11
   49    43    >   ASSIGN_REF                                               !9, !11
   50    44      > JMPNZ                                                    !7, ->28
   52    45    >   ASSIGN_DIM                                               !11, 'products'
         46        OP_DATA                                                  !5
   31    47      > JMP                                                      ->19
         48    >   FE_FREE                                                  $24
   55    49        INIT_FCALL                                               'json_encode'
         50        SEND_VAR                                                 !3
         51        SEND_VAL                                                 128
         52        DO_ICALL                                         $45     
         53        ECHO                                                     $45
         54      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
147.18 ms | 1017 KiB | 16 Q