3v4l.org

run code in 300+ PHP versions simultaneously
<?php $text = "Rocket abc bla blub bla Rocket"; class CountWordSimas { /* * @param string $text */ public function countWordSort($text) { $textArray = explode(" ", $text); $wordCountArray = $this->countWords($textArray); $sortedArray = $this->sortAndReverseArray($wordCountArray); $this->printWordsCount($sortedArray); } /* * Sort the array and reverse * @return array The array sorted and reversed */ protected function sortAndReverseArray($array) { natcasesort($array); $reversedArray = $this->reverseArray($array); krsort($reversedArray); return $reversedArray; } /* * Count how many time the word appear in the array * @param array $textArray * @return array */ protected function countWords($textArray) { $wordCountArray = []; foreach ($textArray as $word) { if (!isset($wordCountArray[$word])) { $wordCountArray[$word] = 1; } else { $wordCountArray[$word]++; } } return $wordCountArray; } /* * @param array $arrayToReverse * @return array The reversed array */ protected function reverseArray($arrayToReverse) { $reversedArray = []; foreach ($arrayToReverse as $key => $value) { $reversedArray[$value][] = $key; } return $reversedArray; } /* * Print the array of words with the count value * @param array $wordsCountArray */ protected function printWordsCount($wordsCountArray) { foreach ($wordsCountArray as $count => $words) { foreach ($words as $word) { echo $count . " x " . $word; echo PHP_EOL; } } } } $simas = new CountWordSimas(); $simas->countWordSort($text);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/egGmI
function name:  (null)
number of ops:  8
compiled vars:  !0 = $text, !1 = $simas
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, 'Rocket+abc+bla+blub+bla+Rocket'
   84     1        NEW                                              $3      'CountWordSimas'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $3
   85     4        INIT_METHOD_CALL                                         !1, 'countWordSort'
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0          
          7      > RETURN                                                   1

Class CountWordSimas:
Function countwordsort:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/egGmI
function name:  countWordSort
number of ops:  18
compiled vars:  !0 = $text, !1 = $textArray, !2 = $wordCountArray, !3 = $sortedArray
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
   12     1        INIT_FCALL                                               'explode'
          2        SEND_VAL                                                 '+'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $4      
          5        ASSIGN                                                   !1, $4
   14     6        INIT_METHOD_CALL                                         'countWords'
          7        SEND_VAR_EX                                              !1
          8        DO_FCALL                                      0  $6      
          9        ASSIGN                                                   !2, $6
   16    10        INIT_METHOD_CALL                                         'sortAndReverseArray'
         11        SEND_VAR_EX                                              !2
         12        DO_FCALL                                      0  $8      
         13        ASSIGN                                                   !3, $8
   18    14        INIT_METHOD_CALL                                         'printWordsCount'
         15        SEND_VAR_EX                                              !3
         16        DO_FCALL                                      0          
   19    17      > RETURN                                                   null

End of function countwordsort

Function sortandreversearray:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/egGmI
function name:  sortAndReverseArray
number of ops:  13
compiled vars:  !0 = $array, !1 = $reversedArray
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   RECV                                             !0      
   27     1        INIT_FCALL                                               'natcasesort'
          2        SEND_REF                                                 !0
          3        DO_ICALL                                                 
   29     4        INIT_METHOD_CALL                                         'reverseArray'
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0  $3      
          7        ASSIGN                                                   !1, $3
   31     8        INIT_FCALL                                               'krsort'
          9        SEND_REF                                                 !1
         10        DO_ICALL                                                 
   33    11      > RETURN                                                   !1
   34    12*     > RETURN                                                   null

End of function sortandreversearray

Function countwords:
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 = 7, Position 2 = 10
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
filename:       /in/egGmI
function name:  countWords
number of ops:  16
compiled vars:  !0 = $textArray, !1 = $wordCountArray, !2 = $word
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E >   RECV                                             !0      
   43     1        ASSIGN                                                   !1, <array>
   44     2      > FE_RESET_R                                       $4      !0, ->13
          3    > > FE_FETCH_R                                               $4, !2, ->13
   45     4    >   ISSET_ISEMPTY_DIM_OBJ                         0  ~5      !1, !2
          5        BOOL_NOT                                         ~6      ~5
          6      > JMPZ                                                     ~6, ->10
   46     7    >   ASSIGN_DIM                                               !1, !2
          8        OP_DATA                                                  1
          9      > JMP                                                      ->12
   48    10    >   FETCH_DIM_RW                                     $8      !1, !2
         11        PRE_INC                                                  $8
   44    12    > > JMP                                                      ->3
         13    >   FE_FREE                                                  $4
   52    14      > RETURN                                                   !1
   53    15*     > RETURN                                                   null

End of function countwords

Function reversearray:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 9
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 9
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
filename:       /in/egGmI
function name:  reverseArray
number of ops:  12
compiled vars:  !0 = $arrayToReverse, !1 = $reversedArray, !2 = $value, !3 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   59     0  E >   RECV                                             !0      
   61     1        ASSIGN                                                   !1, <array>
   62     2      > FE_RESET_R                                       $5      !0, ->9
          3    > > FE_FETCH_R                                       ~6      $5, !2, ->9
          4    >   ASSIGN                                                   !3, ~6
   63     5        FETCH_DIM_W                                      $8      !1, !2
          6        ASSIGN_DIM                                               $8
          7        OP_DATA                                                  !3
   62     8      > JMP                                                      ->3
          9    >   FE_FREE                                                  $5
   66    10      > RETURN                                                   !1
   67    11*     > RETURN                                                   null

End of function reversearray

Function printwordscount:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 13
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 13
Branch analysis from position: 3
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 11
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 11
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 11
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
filename:       /in/egGmI
function name:  printWordsCount
number of ops:  15
compiled vars:  !0 = $wordsCountArray, !1 = $words, !2 = $count, !3 = $word
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   73     0  E >   RECV                                             !0      
   75     1      > FE_RESET_R                                       $4      !0, ->13
          2    > > FE_FETCH_R                                       ~5      $4, !1, ->13
          3    >   ASSIGN                                                   !2, ~5
   76     4      > FE_RESET_R                                       $7      !1, ->11
          5    > > FE_FETCH_R                                               $7, !3, ->11
   77     6    >   CONCAT                                           ~8      !2, '+x+'
          7        CONCAT                                           ~9      ~8, !3
          8        ECHO                                                     ~9
   78     9        ECHO                                                     '%0A'
   76    10      > JMP                                                      ->5
         11    >   FE_FREE                                                  $7
   75    12      > JMP                                                      ->2
         13    >   FE_FREE                                                  $4
   81    14      > RETURN                                                   null

End of function printwordscount

End of class CountWordSimas.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
149.74 ms | 1404 KiB | 19 Q