3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Returns the total number of $count-length strings generatable from $letters. function getPermCount($letters, $count) { $result = 1; // k characters from a set of n has n!/(n-k)! possible combinations for($i = strlen($letters) - $count + 1; $i <= strlen($letters); $i++) { $result *= $i; } return $result; } // Decodes $index to a $count-length string from $letters, no repeat chars. function getPerm($letters, $count, $index) { $result = ''; for($i = 0; $i < $count; $i++) { $pos = $index % strlen($letters); $result .= ",".$letters[$pos]; $index = ($index-$pos)/strlen($letters); $letters = substr($letters, 0, $pos) . substr($letters, $pos+1); } return trim($result, ","); } $abc = array(); $cars=array("a","b","c"); sort($cars); $abc[] = $cars; $cars=array("a","c","b"); sort($cars); $abc[] = $cars; print_r(array_unique($abc, SORT_REGULAR)); $letters = 'abc'; $r = array(); //echo '1 letters from 4: '; for($i = 0; $i < getPermCount($letters, 1); $i++) $r[] = getPerm($letters, 1, $i); //echo '2 letters from 4: '; for($i = 0; $i < getPermCount($letters, 2); $i++) $r[] = getPerm($letters, 2, $i); //echo ' 3 letters from 4: '; for($i = 0; $i < getPermCount($letters, 3); $i++) $r[] = getPerm($letters, 3, $i); print_r($r);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
2 jumps found. (Code = 44) Position 1 = 38, Position 2 = 24
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 48
Branch analysis from position: 48
2 jumps found. (Code = 44) Position 1 = 54, Position 2 = 40
Branch analysis from position: 54
1 jumps found. (Code = 42) Position 1 = 64
Branch analysis from position: 64
2 jumps found. (Code = 44) Position 1 = 70, Position 2 = 56
Branch analysis from position: 70
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 56
2 jumps found. (Code = 44) Position 1 = 70, Position 2 = 56
Branch analysis from position: 70
Branch analysis from position: 56
Branch analysis from position: 40
2 jumps found. (Code = 44) Position 1 = 54, Position 2 = 40
Branch analysis from position: 54
Branch analysis from position: 40
Branch analysis from position: 24
2 jumps found. (Code = 44) Position 1 = 38, Position 2 = 24
Branch analysis from position: 38
Branch analysis from position: 24
filename:       /in/mmBTL
function name:  (null)
number of ops:  74
compiled vars:  !0 = $abc, !1 = $cars, !2 = $letters, !3 = $r, !4 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   ASSIGN                                                   !0, <array>
   28     1        ASSIGN                                                   !1, <array>
   29     2        INIT_FCALL                                               'sort'
          3        SEND_REF                                                 !1
          4        DO_ICALL                                                 
   30     5        ASSIGN_DIM                                               !0
          6        OP_DATA                                                  !1
   32     7        ASSIGN                                                   !1, <array>
   33     8        INIT_FCALL                                               'sort'
          9        SEND_REF                                                 !1
         10        DO_ICALL                                                 
   34    11        ASSIGN_DIM                                               !0
         12        OP_DATA                                                  !1
   36    13        INIT_FCALL                                               'print_r'
         14        INIT_FCALL                                               'array_unique'
         15        SEND_VAR                                                 !0
         16        SEND_VAL                                                 0
         17        DO_ICALL                                         $12     
         18        SEND_VAR                                                 $12
         19        DO_ICALL                                                 
   38    20        ASSIGN                                                   !2, 'abc'
   39    21        ASSIGN                                                   !3, <array>
   42    22        ASSIGN                                                   !4, 0
         23      > JMP                                                      ->32
   43    24    >   INIT_FCALL                                               'getperm'
         25        SEND_VAR                                                 !2
         26        SEND_VAL                                                 1
         27        SEND_VAR                                                 !4
         28        DO_FCALL                                      0  $18     
         29        ASSIGN_DIM                                               !3
         30        OP_DATA                                                  $18
   42    31        PRE_INC                                                  !4
         32    >   INIT_FCALL                                               'getpermcount'
         33        SEND_VAR                                                 !2
         34        SEND_VAL                                                 1
         35        DO_FCALL                                      0  $20     
         36        IS_SMALLER                                               !4, $20
         37      > JMPNZ                                                    ~21, ->24
   46    38    >   ASSIGN                                                   !4, 0
         39      > JMP                                                      ->48
   47    40    >   INIT_FCALL                                               'getperm'
         41        SEND_VAR                                                 !2
         42        SEND_VAL                                                 2
         43        SEND_VAR                                                 !4
         44        DO_FCALL                                      0  $24     
         45        ASSIGN_DIM                                               !3
         46        OP_DATA                                                  $24
   46    47        PRE_INC                                                  !4
         48    >   INIT_FCALL                                               'getpermcount'
         49        SEND_VAR                                                 !2
         50        SEND_VAL                                                 2
         51        DO_FCALL                                      0  $26     
         52        IS_SMALLER                                               !4, $26
         53      > JMPNZ                                                    ~27, ->40
   50    54    >   ASSIGN                                                   !4, 0
         55      > JMP                                                      ->64
   51    56    >   INIT_FCALL                                               'getperm'
         57        SEND_VAR                                                 !2
         58        SEND_VAL                                                 3
         59        SEND_VAR                                                 !4
         60        DO_FCALL                                      0  $30     
         61        ASSIGN_DIM                                               !3
         62        OP_DATA                                                  $30
   50    63        PRE_INC                                                  !4
         64    >   INIT_FCALL                                               'getpermcount'
         65        SEND_VAR                                                 !2
         66        SEND_VAL                                                 3
         67        DO_FCALL                                      0  $32     
         68        IS_SMALLER                                               !4, $32
         69      > JMPNZ                                                    ~33, ->56
   53    70    >   INIT_FCALL                                               'print_r'
         71        SEND_VAR                                                 !3
         72        DO_ICALL                                                 
         73      > RETURN                                                   1

Function getpermcount:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
2 jumps found. (Code = 44) Position 1 = 13, Position 2 = 8
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 44) Position 1 = 13, Position 2 = 8
Branch analysis from position: 13
Branch analysis from position: 8
filename:       /in/mmBTL
function name:  getPermCount
number of ops:  15
compiled vars:  !0 = $letters, !1 = $count, !2 = $result, !3 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    5     2        ASSIGN                                                   !2, 1
    7     3        STRLEN                                           ~5      !0
          4        SUB                                              ~6      ~5, !1
          5        ADD                                              ~7      ~6, 1
          6        ASSIGN                                                   !3, ~7
          7      > JMP                                                      ->10
    8     8    >   ASSIGN_OP                                     3          !2, !3
    7     9        PRE_INC                                                  !3
         10    >   STRLEN                                           ~11     !0
         11        IS_SMALLER_OR_EQUAL                                      !3, ~11
         12      > JMPNZ                                                    ~12, ->8
   10    13    > > RETURN                                                   !2
   11    14*     > RETURN                                                   null

End of function getpermcount

Function getperm:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
2 jumps found. (Code = 44) Position 1 = 31, Position 2 = 6
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
2 jumps found. (Code = 44) Position 1 = 31, Position 2 = 6
Branch analysis from position: 31
Branch analysis from position: 6
filename:       /in/mmBTL
function name:  getPerm
number of ops:  37
compiled vars:  !0 = $letters, !1 = $count, !2 = $index, !3 = $result, !4 = $i, !5 = $pos
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   16     3        ASSIGN                                                   !3, ''
   17     4        ASSIGN                                                   !4, 0
          5      > JMP                                                      ->29
   19     6    >   STRLEN                                           ~8      !0
          7        MOD                                              ~9      !2, ~8
          8        ASSIGN                                                   !5, ~9
   20     9        FETCH_DIM_R                                      ~11     !0, !5
         10        CONCAT                                           ~12     '%2C', ~11
         11        ASSIGN_OP                                     8          !3, ~12
   21    12        SUB                                              ~14     !2, !5
         13        STRLEN                                           ~15     !0
         14        DIV                                              ~16     ~14, ~15
         15        ASSIGN                                                   !2, ~16
   22    16        INIT_FCALL                                               'substr'
         17        SEND_VAR                                                 !0
         18        SEND_VAL                                                 0
         19        SEND_VAR                                                 !5
         20        DO_ICALL                                         $18     
         21        INIT_FCALL                                               'substr'
         22        SEND_VAR                                                 !0
         23        ADD                                              ~19     !5, 1
         24        SEND_VAL                                                 ~19
         25        DO_ICALL                                         $20     
         26        CONCAT                                           ~21     $18, $20
         27        ASSIGN                                                   !0, ~21
   17    28        PRE_INC                                                  !4
         29    >   IS_SMALLER                                               !4, !1
         30      > JMPNZ                                                    ~24, ->6
   24    31    >   INIT_FCALL                                               'trim'
         32        SEND_VAR                                                 !3
         33        SEND_VAL                                                 '%2C'
         34        DO_ICALL                                         $25     
         35      > RETURN                                                   $25
   25    36*     > RETURN                                                   null

End of function getperm

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
164.04 ms | 1411 KiB | 29 Q