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 { protected $_depthArray; protected function _saveDepthKey($key, $depth) { if(!isset($this->_depthArray[$depth])) { $this->_depthArray[$depth] = array(); } if(!in_array($key, $this->_depthArray[$depth])) { $this->_depthArray[$depth][] = $key; return true; } return false; } 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); $depth = count($array); if($depth == 1) { $stackedArray[$key] = $array[0]; // that's a single value } else { $subArray = $this->_stack($array); if($subArray != null) { $stackedArray[$key] = $subArray; } } // save key to find duplicated if(!$this->_saveDepthKey($key, $depth)) return null; 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/D4VWs
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'
   90     1        NEW                                              $3      'ArrayStacker'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $3
   91     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 _savedepthkey:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 9
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 22
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
filename:       /in/D4VWs
function name:  _saveDepthKey
number of ops:  24
compiled vars:  !0 = $key, !1 = $depth
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   23     2        FETCH_OBJ_IS                                     ~2      '_depthArray'
          3        ISSET_ISEMPTY_DIM_OBJ                         0  ~3      ~2, !1
          4        BOOL_NOT                                         ~4      ~3
          5      > JMPZ                                                     ~4, ->9
   24     6    >   FETCH_OBJ_W                                      $5      '_depthArray'
          7        ASSIGN_DIM                                               $5, !1
          8        OP_DATA                                                  <array>
   26     9    >   INIT_FCALL                                               'in_array'
         10        SEND_VAR                                                 !0
         11        FETCH_OBJ_R                                      ~7      '_depthArray'
         12        FETCH_DIM_R                                      ~8      ~7, !1
         13        SEND_VAL                                                 ~8
         14        DO_ICALL                                         $9      
         15        BOOL_NOT                                         ~10     $9
         16      > JMPZ                                                     ~10, ->22
   27    17    >   FETCH_OBJ_W                                      $11     '_depthArray'
         18        FETCH_DIM_W                                      $12     $11, !1
         19        ASSIGN_DIM                                               $12
         20        OP_DATA                                                  !0
   28    21      > RETURN                                                   <true>
   30    22    > > RETURN                                                   <false>
   31    23*     > RETURN                                                   null

End of function _savedepthkey

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/D4VWs
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
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
   34     1        INIT_METHOD_CALL                                         '_splitString'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0  $4      
          4        ASSIGN                                                   !1, $4
   35     5        ASSIGN                                                   !2, <array>
   36     6      > FE_RESET_R                                       $7      !1, ->17
          7    > > FE_FETCH_R                                               $7, !3, ->17
   37     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
   36    16      > JMP                                                      ->7
         17    >   FE_FREE                                                  $7
   39    18      > RETURN                                                   !2
   40    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/D4VWs
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
-------------------------------------------------------------------------------------
   48     0  E >   RECV                                             !0      
   49     1        INIT_FCALL                                               'explode'
          2        SEND_VAL                                                 '%0A'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $8      
          5        ASSIGN                                                   !1, $8
   50     6        ASSIGN                                                   !2, 0
   51     7        ASSIGN                                                   !3, <array>
   52     8      > FE_RESET_R                                       $12     !1, ->29
          9    > > FE_FETCH_R                                               $12, !4, ->29
   53    10    >   INIT_FCALL                                               'explode'
         11        SEND_VAL                                                 '+%2F+'
         12        SEND_VAR                                                 !4
         13        DO_ICALL                                         $13     
         14        ASSIGN                                                   !5, $13
   54    15      > FE_RESET_R                                       $15     !5, ->24
         16    > > FE_FETCH_R                                       ~16     $15, !6, ->24
         17    >   ASSIGN                                                   !7, ~16
   55    18        INIT_FCALL                                               'trim'
         19        SEND_VAR                                                 !6
         20        DO_ICALL                                         $19     
         21        ASSIGN_DIM                                               !5, !7
         22        OP_DATA                                                  $19
   54    23      > JMP                                                      ->16
         24    >   FE_FREE                                                  $15
   57    25        ASSIGN_DIM                                               !3, !2
         26        OP_DATA                                                  !5
   58    27        PRE_INC                                                  !2
   52    28      > JMP                                                      ->9
         29    >   FE_FREE                                                  $12
   60    30      > RETURN                                                   !3
   61    31*     > RETURN                                                   null

End of function _splitstring

Function _stack:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 14
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 22
Branch analysis from position: 22
2 jumps found. (Code = 43) Position 1 = 28, Position 2 = 29
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 22
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 28, Position 2 = 29
Branch analysis from position: 28
Branch analysis from position: 29
Branch analysis from position: 22
filename:       /in/D4VWs
function name:  _stack
number of ops:  31
compiled vars:  !0 = $array, !1 = $stackedArray, !2 = $key, !3 = $depth, !4 = $subArray
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   69     0  E >   RECV                                             !0      
   70     1        ASSIGN                                                   !1, <array>
   71     2        INIT_FCALL                                               'array_shift'
          3        SEND_REF                                                 !0
          4        DO_ICALL                                         $6      
          5        ASSIGN                                                   !2, $6
   72     6        COUNT                                            ~8      !0
          7        ASSIGN                                                   !3, ~8
   74     8        IS_EQUAL                                                 !3, 1
          9      > JMPZ                                                     ~10, ->14
   75    10    >   FETCH_DIM_R                                      ~12     !0, 0
         11        ASSIGN_DIM                                               !1, !2
         12        OP_DATA                                                  ~12
         13      > JMP                                                      ->22
   77    14    >   INIT_METHOD_CALL                                         '_stack'
         15        SEND_VAR_EX                                              !0
         16        DO_FCALL                                      0  $13     
         17        ASSIGN                                                   !4, $13
   78    18        IS_NOT_EQUAL                                             !4, null
         19      > JMPZ                                                     ~15, ->22
   79    20    >   ASSIGN_DIM                                               !1, !2
         21        OP_DATA                                                  !4
   84    22    >   INIT_METHOD_CALL                                         '_saveDepthKey'
         23        SEND_VAR_EX                                              !2
         24        SEND_VAR_EX                                              !3
         25        DO_FCALL                                      0  $17     
         26        BOOL_NOT                                         ~18     $17
         27      > JMPZ                                                     ~18, ->29
         28    > > RETURN                                                   null
   86    29    > > RETURN                                                   !1
   87    30*     > RETURN                                                   null

End of function _stack

End of class ArrayStacker.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
169.41 ms | 1408 KiB | 25 Q