3v4l.org

run code in 300+ PHP versions simultaneously
<?php $string = "Football / Germany / 1.Liga Football / Germany / 1.Liga Football / Germany / 2.Liga Football / Germany / 2.Liga Football / Germany / 2.Liga Football / England / 1.Liga Football / England / 1.Liga Football / England / 2.Liga Football / England / 2.Liga Football / England / 3.Liga Hockey / Germany / 1.Liga Hockey / Germany / 1.Liga Hockey / Germany / 2.Liga Fechten / Meisterschaft Fechten / Meisterschaft Fechten / Weltmeister"; class ArrayStacker { public function stackArray($string) { $array = $this->splitString($string); $stackedArray = array(); foreach($array as $row) { $stackedArray = array_merge_recursive($stackedArray, $this->_stack($row)); } return $stackedArray; } /** * Split the whole string into a big array * * @param string $string * @return array */ protected function splitString($string) { $lines = explode("\n", $string); $i = 0; $array = array(); foreach($lines as $line) { $values = explode(' / ', $line); foreach($values as $v => $value) { $values[$v] = trim($value); // make sure we got no whitespaces } $array[$i] = $values; $i++; } return $array; } /** * Build a stacked array * * @param array $array unstacked array * @return stacked array */ protected function _stack($array) { $stackedArray = array(); $key = array_shift($array); if(count($array) == 1) { $stackedArray[$key] = $array[0]; // that's a single value } else { $stackedArray[$key] = $this->_stack($array); } return $stackedArray; } } $arrayStacker = new ArrayStacker(); var_dump($arrayStacker->stackArray($string));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XmpX8
function name:  (null)
number of ops:  11
compiled vars:  !0 = $string, !1 = $arrayStacker
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, 'Football+%2F+Germany+%2F+1.Liga%0AFootball+%2F+Germany+%2F+1.Liga%0AFootball+%2F+Germany+%2F+2.Liga%0AFootball+%2F+Germany+%2F+2.Liga%0AFootball+%2F+Germany+%2F+2.Liga%0AFootball+%2F+England+%2F+1.Liga%0AFootball+%2F+England+%2F+1.Liga%0AFootball+%2F+England+%2F+2.Liga%0AFootball+%2F+England+%2F+2.Liga%0AFootball+%2F+England+%2F+3.Liga%0AHockey+%2F+Germany+%2F+1.Liga%0AHockey+%2F+Germany+%2F+1.Liga%0AHockey+%2F+Germany+%2F+2.Liga%0AFechten+%2F+Meisterschaft%0AFechten+%2F+Meisterschaft%0AFechten+%2F+Weltmeister'
   69     1        NEW                                              $3      'ArrayStacker'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $3
   70     4        INIT_FCALL                                               'var_dump'
          5        INIT_METHOD_CALL                                         !1, 'stackArray'
          6        SEND_VAR_EX                                              !0
          7        DO_FCALL                                      0  $6      
          8        SEND_VAR                                                 $6
          9        DO_ICALL                                                 
         10      > RETURN                                                   1

Class ArrayStacker:
Function stackarray:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 17
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 17
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
filename:       /in/XmpX8
function name:  stackArray
number of ops:  20
compiled vars:  !0 = $string, !1 = $array, !2 = $stackedArray, !3 = $row
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   RECV                                             !0      
   22     1        INIT_METHOD_CALL                                         'splitString'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0  $4      
          4        ASSIGN                                                   !1, $4
   23     5        ASSIGN                                                   !2, <array>
   24     6      > FE_RESET_R                                       $7      !1, ->17
          7    > > FE_FETCH_R                                               $7, !3, ->17
   25     8    >   INIT_FCALL                                               'array_merge_recursive'
          9        SEND_VAR                                                 !2
         10        INIT_METHOD_CALL                                         '_stack'
         11        SEND_VAR_EX                                              !3
         12        DO_FCALL                                      0  $8      
         13        SEND_VAR                                                 $8
         14        DO_ICALL                                         $9      
         15        ASSIGN                                                   !2, $9
   24    16      > JMP                                                      ->7
         17    >   FE_FREE                                                  $7
   27    18      > RETURN                                                   !2
   28    19*     > RETURN                                                   null

End of function stackarray

Function splitstring:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 9, Position 2 = 29
Branch analysis from position: 9
2 jumps found. (Code = 78) Position 1 = 10, Position 2 = 29
Branch analysis from position: 10
2 jumps found. (Code = 77) Position 1 = 16, Position 2 = 24
Branch analysis from position: 16
2 jumps found. (Code = 78) Position 1 = 17, Position 2 = 24
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
Branch analysis from position: 24
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 29
filename:       /in/XmpX8
function name:  splitString
number of ops:  32
compiled vars:  !0 = $string, !1 = $lines, !2 = $i, !3 = $array, !4 = $line, !5 = $values, !6 = $value, !7 = $v
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   RECV                                             !0      
   37     1        INIT_FCALL                                               'explode'
          2        SEND_VAL                                                 '%0A'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $8      
          5        ASSIGN                                                   !1, $8
   38     6        ASSIGN                                                   !2, 0
   39     7        ASSIGN                                                   !3, <array>
   40     8      > FE_RESET_R                                       $12     !1, ->29
          9    > > FE_FETCH_R                                               $12, !4, ->29
   41    10    >   INIT_FCALL                                               'explode'
         11        SEND_VAL                                                 '+%2F+'
         12        SEND_VAR                                                 !4
         13        DO_ICALL                                         $13     
         14        ASSIGN                                                   !5, $13
   42    15      > FE_RESET_R                                       $15     !5, ->24
         16    > > FE_FETCH_R                                       ~16     $15, !6, ->24
         17    >   ASSIGN                                                   !7, ~16
   43    18        INIT_FCALL                                               'trim'
         19        SEND_VAR                                                 !6
         20        DO_ICALL                                         $19     
         21        ASSIGN_DIM                                               !5, !7
         22        OP_DATA                                                  $19
   42    23      > JMP                                                      ->16
         24    >   FE_FREE                                                  $15
   45    25        ASSIGN_DIM                                               !3, !2
         26        OP_DATA                                                  !5
   46    27        PRE_INC                                                  !2
   40    28      > JMP                                                      ->9
         29    >   FE_FREE                                                  $12
   48    30      > RETURN                                                   !3
   49    31*     > RETURN                                                   null

End of function splitstring

Function _stack:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 13
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XmpX8
function name:  _stack
number of ops:  20
compiled vars:  !0 = $array, !1 = $stackedArray, !2 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   57     0  E >   RECV                                             !0      
   58     1        ASSIGN                                                   !1, <array>
   59     2        INIT_FCALL                                               'array_shift'
          3        SEND_REF                                                 !0
          4        DO_ICALL                                         $4      
          5        ASSIGN                                                   !2, $4
   60     6        COUNT                                            ~6      !0
          7        IS_EQUAL                                                 ~6, 1
          8      > JMPZ                                                     ~7, ->13
   61     9    >   FETCH_DIM_R                                      ~9      !0, 0
         10        ASSIGN_DIM                                               !1, !2
         11        OP_DATA                                                  ~9
         12      > JMP                                                      ->18
   63    13    >   INIT_METHOD_CALL                                         '_stack'
         14        SEND_VAR_EX                                              !0
         15        DO_FCALL                                      0  $11     
         16        ASSIGN_DIM                                               !1, !2
         17        OP_DATA                                                  $11
   65    18    > > RETURN                                                   !1
   66    19*     > RETURN                                                   null

End of function _stack

End of class ArrayStacker.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
148.11 ms | 1404 KiB | 23 Q