3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * ArrayStacker */ class ArrayStacker { /** * Find double values in array and remove them * @param array $array */ protected function _filterArrayDistict($array) { $newArray = array(); foreach($array as $value) { if(!in_array($value, $newArray)) { $newArray[] = $value; } } return $newArray; } /** * 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; } } return $stackedArray; } /** * Build stacked array from string * * @param string $string * @return array stacked array */ public function stackArray($string) { // Split the string to an array $array = $this->_splitString($string); // Filter out doubled values $array = $this->_filterArrayDistict($array); // Build stacked array $stackedArray = array(); foreach($array as $row) { $stackedArray = array_merge_recursive($stackedArray, $this->_stack($row)); } return $stackedArray; } } $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"; $arrayStacker = new ArrayStacker(); $stackedArray = $arrayStacker->stackArray($string); var_dump($stackedArray); /* * OUTPUT: array(3) { ["Football"]=> array(2) { ["Germany"]=> array(2) { [0]=> string(6) "1.Liga" [1]=> string(6) "2.Liga" } ["England"]=> array(3) { [0]=> string(6) "1.Liga" [1]=> string(6) "2.Liga" [2]=> string(6) "3.Liga" } } ["Hockey"]=> array(1) { ["Germany"]=> array(2) { [0]=> string(6) "1.Liga" [1]=> string(6) "2.Liga" } } ["Fechten"]=> array(2) { [0]=> string(13) "Meisterschaft" [1]=> string(11) "Weltmeister" } } */
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/GvNcl
function name:  (null)
number of ops:  12
compiled vars:  !0 = $string, !1 = $arrayStacker, !2 = $stackedArray
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   90     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'
  107     1        NEW                                              $4      'ArrayStacker'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $4
  108     4        INIT_METHOD_CALL                                         !1, 'stackArray'
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0  $7      
          7        ASSIGN                                                   !2, $7
  109     8        INIT_FCALL                                               'var_dump'
          9        SEND_VAR                                                 !2
         10        DO_ICALL                                                 
  153    11      > RETURN                                                   1

Class ArrayStacker:
Function _filterarraydistict:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 13
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 13
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 12
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 12
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
filename:       /in/GvNcl
function name:  _filterArrayDistict
number of ops:  16
compiled vars:  !0 = $array, !1 = $newArray, !2 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   RECV                                             !0      
   12     1        ASSIGN                                                   !1, <array>
   14     2      > FE_RESET_R                                       $4      !0, ->13
          3    > > FE_FETCH_R                                               $4, !2, ->13
   15     4    >   INIT_FCALL                                               'in_array'
          5        SEND_VAR                                                 !2
          6        SEND_VAR                                                 !1
          7        DO_ICALL                                         $5      
          8        BOOL_NOT                                         ~6      $5
          9      > JMPZ                                                     ~6, ->12
   16    10    >   ASSIGN_DIM                                               !1
         11        OP_DATA                                                  !2
   14    12    > > JMP                                                      ->3
         13    >   FE_FREE                                                  $4
   19    14      > RETURN                                                   !1
   20    15*     > RETURN                                                   null

End of function _filterarraydistict

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/GvNcl
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
-------------------------------------------------------------------------------------
   28     0  E >   RECV                                             !0      
   29     1        INIT_FCALL                                               'explode'
          2        SEND_VAL                                                 '%0A'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $8      
          5        ASSIGN                                                   !1, $8
   30     6        ASSIGN                                                   !2, 0
   31     7        ASSIGN                                                   !3, <array>
   32     8      > FE_RESET_R                                       $12     !1, ->29
          9    > > FE_FETCH_R                                               $12, !4, ->29
   33    10    >   INIT_FCALL                                               'explode'
         11        SEND_VAL                                                 '+%2F+'
         12        SEND_VAR                                                 !4
         13        DO_ICALL                                         $13     
         14        ASSIGN                                                   !5, $13
   34    15      > FE_RESET_R                                       $15     !5, ->24
         16    > > FE_FETCH_R                                       ~16     $15, !6, ->24
         17    >   ASSIGN                                                   !7, ~16
   35    18        INIT_FCALL                                               'trim'
         19        SEND_VAR                                                 !6
         20        DO_ICALL                                         $19     
         21        ASSIGN_DIM                                               !5, !7
         22        OP_DATA                                                  $19
   34    23      > JMP                                                      ->16
         24    >   FE_FREE                                                  $15
   37    25        ASSIGN_DIM                                               !3, !2
         26        OP_DATA                                                  !5
   38    27        PRE_INC                                                  !2
   32    28      > JMP                                                      ->9
         29    >   FE_FREE                                                  $12
   40    30      > RETURN                                                   !3
   41    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 = 15
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 24
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
filename:       /in/GvNcl
function name:  _stack
number of ops:  26
compiled vars:  !0 = $array, !1 = $stackedArray, !2 = $key, !3 = $depth, !4 = $subArray
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   49     0  E >   RECV                                             !0      
   50     1        ASSIGN                                                   !1, <array>
   51     2        INIT_FCALL                                               'array_shift'
          3        SEND_REF                                                 !0
          4        DO_ICALL                                         $6      
          5        ASSIGN                                                   !2, $6
   52     6        COUNT                                            ~8      !0
          7        ASSIGN                                                   !3, ~8
   54     8        IS_EQUAL                                                 !3, 1
          9      > JMPZ                                                     ~10, ->15
   55    10    >   FETCH_DIM_R                                      ~13     !0, 0
         11        FETCH_DIM_W                                      $11     !1
         12        ASSIGN_DIM                                               $11, !2
         13        OP_DATA                                                  ~13
         14      > JMP                                                      ->24
   57    15    >   INIT_METHOD_CALL                                         '_stack'
         16        SEND_VAR_EX                                              !0
         17        DO_FCALL                                      0  $14     
         18        ASSIGN                                                   !4, $14
   58    19        IS_NOT_EQUAL                                             !4, null
         20      > JMPZ                                                     ~16, ->24
   59    21    >   FETCH_DIM_W                                      $17     !1
         22        ASSIGN_DIM                                               $17, !2
         23        OP_DATA                                                  !4
   63    24    > > RETURN                                                   !1
   64    25*     > RETURN                                                   null

End of function _stack

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

End of function stackarray

End of class ArrayStacker.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
174.03 ms | 1400 KiB | 25 Q