3v4l.org

run code in 300+ PHP versions simultaneously
<?php function valueSeparator(array $array) { $maxIterations = count($array); $counted = array_count_values($array); arsort($counted); $iteration = 0; $result = []; while (max($counted) > 1 && $iteration < $maxIterations) { $count = reset($counted); $value = key($counted); $result[] = $value; unset($counted[$value]); arsort($counted); if ($count > 1) { $counted = array_merge( array_splice($counted, 0, 1), [$value => $count - 1], $counted ); } ++$iteration; } array_push($result, ...array_keys($counted)); var_export($result); } $arrays = [ ['Retail', 'Retail', 'Food', 'Food', 'Retail'], ['Retail', 'Retail', 'Food', 'Charity', 'Entertainment', 'Entertainment', 'Transport', 'Cosmetics'], ['Food'], ['Retail', 'Retail', 'Food', 'Retail'], ['Retail', 'Retail', 'Retail', 'Food', 'Food', 'Food', 'Charity', 'Charity', 'Charity'], ['Charity', 'Entertainment', 'Retail', 'Retail' ,'Retail'] ]; foreach ($arrays as $array) { valueSeparator($array); echo "\n---\n"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 8
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 8
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
filename:       /in/M4huo
function name:  (null)
number of ops:  10
compiled vars:  !0 = $arrays, !1 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   ASSIGN                                                   !0, <array>
   37     1      > FE_RESET_R                                       $3      !0, ->8
          2    > > FE_FETCH_R                                               $3, !1, ->8
   38     3    >   INIT_FCALL                                               'valueseparator'
          4        SEND_VAR                                                 !1
          5        DO_FCALL                                      0          
   39     6        ECHO                                                     '%0A---%0A'
   37     7      > JMP                                                      ->2
          8    >   FE_FREE                                                  $3
   40     9      > RETURN                                                   1

Function valueseparator:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
2 jumps found. (Code = 46) Position 1 = 48, Position 2 = 50
Branch analysis from position: 48
2 jumps found. (Code = 44) Position 1 = 51, Position 2 = 13
Branch analysis from position: 51
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 42
Branch analysis from position: 29
2 jumps found. (Code = 46) Position 1 = 48, Position 2 = 50
Branch analysis from position: 48
Branch analysis from position: 50
Branch analysis from position: 42
Branch analysis from position: 50
filename:       /in/M4huo
function name:  valueSeparator
number of ops:  63
compiled vars:  !0 = $array, !1 = $maxIterations, !2 = $counted, !3 = $iteration, !4 = $result, !5 = $count, !6 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    4     1        COUNT                                            ~7      !0
          2        ASSIGN                                                   !1, ~7
    5     3        INIT_FCALL                                               'array_count_values'
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $9      
          6        ASSIGN                                                   !2, $9
    6     7        INIT_FCALL                                               'arsort'
          8        SEND_REF                                                 !2
          9        DO_ICALL                                                 
    7    10        ASSIGN                                                   !3, 0
    8    11        ASSIGN                                                   !4, <array>
    9    12      > JMP                                                      ->43
   10    13    >   INIT_FCALL                                               'reset'
         14        SEND_REF                                                 !2
         15        DO_ICALL                                         $14     
         16        ASSIGN                                                   !5, $14
   11    17        INIT_FCALL                                               'key'
         18        SEND_VAR                                                 !2
         19        DO_ICALL                                         $16     
         20        ASSIGN                                                   !6, $16
   12    21        ASSIGN_DIM                                               !4
         22        OP_DATA                                                  !6
   13    23        UNSET_DIM                                                !2, !6
   14    24        INIT_FCALL                                               'arsort'
         25        SEND_REF                                                 !2
         26        DO_ICALL                                                 
   15    27        IS_SMALLER                                               1, !5
         28      > JMPZ                                                     ~20, ->42
   16    29    >   INIT_FCALL                                               'array_merge'
   17    30        INIT_FCALL                                               'array_splice'
         31        SEND_REF                                                 !2
         32        SEND_VAL                                                 0
         33        SEND_VAL                                                 1
         34        DO_ICALL                                         $21     
         35        SEND_VAR                                                 $21
   18    36        SUB                                              ~22     !5, 1
         37        INIT_ARRAY                                       ~23     ~22, !6
         38        SEND_VAL                                                 ~23
   19    39        SEND_VAR                                                 !2
         40        DO_ICALL                                         $24     
   16    41        ASSIGN                                                   !2, $24
   22    42    >   PRE_INC                                                  !3
    9    43    >   INIT_FCALL                                               'max'
         44        SEND_VAR                                                 !2
         45        DO_ICALL                                         $27     
         46        IS_SMALLER                                       ~28     1, $27
         47      > JMPZ_EX                                          ~28     ~28, ->50
         48    >   IS_SMALLER                                       ~29     !3, !1
         49        BOOL                                             ~28     ~29
         50    > > JMPNZ                                                    ~28, ->13
   24    51    >   INIT_FCALL                                               'array_push'
         52        SEND_REF                                                 !4
         53        INIT_FCALL                                               'array_keys'
         54        SEND_VAR                                                 !2
         55        DO_ICALL                                         $30     
         56        SEND_UNPACK                                              $30
         57        CHECK_UNDEF_ARGS                                         
         58        DO_ICALL                                                 
   25    59        INIT_FCALL                                               'var_export'
         60        SEND_VAR                                                 !4
         61        DO_ICALL                                                 
   26    62      > RETURN                                                   null

End of function valueseparator

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
181.7 ms | 1407 KiB | 34 Q