3v4l.org

run code in 300+ PHP versions simultaneously
<?php $input = [ 'foo.bar', 'foo.bar.baz', 'foo.bar.qux', 'foo.qux', 'foo.quux', 'bar.baz', 'bar.qux', 'qux.quux', ]; $output=[]; foreach ($input as $line){ $keys = explode('.',$line); //break each section into a key $val = true; //holds next value to add to array $localArray = []; //holds the array for this input line for($i=count($keys)-1; $i>=0; $i--){ //go in reverse order $localArray = [$keys[$i]=>$val]; //store previous value in array $val = $localArray; //store the array we just built. it will be //the value of the next key } $output = array_merge_recursive($output,$localArray); } print_r($output);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 28
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 28
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
2 jumps found. (Code = 44) Position 1 = 22, Position 2 = 15
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 15
2 jumps found. (Code = 44) Position 1 = 22, Position 2 = 15
Branch analysis from position: 22
Branch analysis from position: 15
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 28
filename:       /in/LB03O
function name:  (null)
number of ops:  33
compiled vars:  !0 = $input, !1 = $output, !2 = $line, !3 = $keys, !4 = $val, !5 = $localArray, !6 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, <array>
   12     1        ASSIGN                                                   !1, <array>
   13     2      > FE_RESET_R                                       $9      !0, ->28
          3    > > FE_FETCH_R                                               $9, !2, ->28
   14     4    >   INIT_FCALL                                               'explode'
          5        SEND_VAL                                                 '.'
          6        SEND_VAR                                                 !2
          7        DO_ICALL                                         $10     
          8        ASSIGN                                                   !3, $10
   15     9        ASSIGN                                                   !4, <true>
   16    10        ASSIGN                                                   !5, <array>
   17    11        COUNT                                            ~14     !3
         12        SUB                                              ~15     ~14, 1
         13        ASSIGN                                                   !6, ~15
         14      > JMP                                                      ->20
   18    15    >   FETCH_DIM_R                                      ~17     !3, !6
         16        INIT_ARRAY                                       ~18     !4, ~17
         17        ASSIGN                                                   !5, ~18
   19    18        ASSIGN                                                   !4, !5
   17    19        PRE_DEC                                                  !6
         20    >   IS_SMALLER_OR_EQUAL                                      0, !6
         21      > JMPNZ                                                    ~22, ->15
   22    22    >   INIT_FCALL                                               'array_merge_recursive'
         23        SEND_VAR                                                 !1
         24        SEND_VAR                                                 !5
         25        DO_ICALL                                         $23     
         26        ASSIGN                                                   !1, $23
   13    27      > JMP                                                      ->3
         28    >   FE_FREE                                                  $9
   24    29        INIT_FCALL                                               'print_r'
         30        SEND_VAR                                                 !1
         31        DO_ICALL                                                 
         32      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
147.64 ms | 1013 KiB | 16 Q