3v4l.org

run code in 300+ PHP versions simultaneously
<?php $data = [ 0 => 'Business Services', 1 => 'Business Services_Consulting', 2 => 'Business Services_Foodservice Distribution', 3 => 'Business Services_Information Services', 4 => 'Consumer', 5 => 'Consumer_Consumer Discretionary', 6 => 'Consumer_Consumer Discretionary_Apparel, Footwear & Accessories', 7 => 'Consumer_Consumer Discretionary_Apparel, Footwear & Accessories_Activewear', 8 => 'Consumer_Consumer Discretionary_Apparel, Footwear & Accessories_Apparel', 9 => 'Consumer_Consumer Discretionary_Apparel, Footwear & Accessories_Footwear', ]; // Our output array $array = []; foreach ($data as $item) { // Store a reference to the "deepest" item (so far) $last = &$array; // Split on known delim and iterate over foreach (explode('_', $item) as $key) { // See next comment for what this does. if (!is_array($last)) { $last = []; } // Set a key to match the value exactly. If the value has "child" objects, the logic // above will take care of removing the value and replacing it with an array. if (!array_key_exists($key, $last)) { $last[$key] = $key; } // Reset our "deepest" item $last = &$last[$key]; } } echo json_encode($array, JSON_PRETTY_PRINT);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 25
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 25
Branch analysis from position: 4
2 jumps found. (Code = 77) Position 1 = 10, Position 2 = 23
Branch analysis from position: 10
2 jumps found. (Code = 78) Position 1 = 11, Position 2 = 23
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 15
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 20
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 20
Branch analysis from position: 15
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 23
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
filename:       /in/UAV72
function name:  (null)
number of ops:  32
compiled vars:  !0 = $data, !1 = $array, !2 = $item, !3 = $last, !4 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   17     1        ASSIGN                                                   !1, <array>
   19     2      > FE_RESET_R                                       $7      !0, ->25
          3    > > FE_FETCH_R                                               $7, !2, ->25
   21     4    >   ASSIGN_REF                                               !3, !1
   24     5        INIT_FCALL                                               'explode'
          6        SEND_VAL                                                 '_'
          7        SEND_VAR                                                 !2
          8        DO_ICALL                                         $9      
          9      > FE_RESET_R                                       $10     $9, ->23
         10    > > FE_FETCH_R                                               $10, !4, ->23
   26    11    >   TYPE_CHECK                                  128  ~11     !3
         12        BOOL_NOT                                         ~12     ~11
         13      > JMPZ                                                     ~12, ->15
   27    14    >   ASSIGN                                                   !3, <array>
   32    15    >   ARRAY_KEY_EXISTS                                 ~14     !4, !3
         16        BOOL_NOT                                         ~15     ~14
         17      > JMPZ                                                     ~15, ->20
   33    18    >   ASSIGN_DIM                                               !3, !4
         19        OP_DATA                                                  !4
   37    20    >   FETCH_DIM_W                                      $17     !3, !4
         21        ASSIGN_REF                                               !3, $17
   24    22      > JMP                                                      ->10
         23    >   FE_FREE                                                  $10
   19    24      > JMP                                                      ->3
         25    >   FE_FREE                                                  $7
   41    26        INIT_FCALL                                               'json_encode'
         27        SEND_VAR                                                 !1
         28        SEND_VAL                                                 128
         29        DO_ICALL                                         $19     
         30        ECHO                                                     $19
         31      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
146.77 ms | 1004 KiB | 15 Q