3v4l.org

run code in 300+ PHP versions simultaneously
<?php $vendors = ["Amazon", "Newegg", "Frys"]; $products = ['a', 'b']; $combinations = array(); // Algorithm to generate all combinations of a specific size from an array of possible values function sampling($chars, $size, $combinations = array()) { # if it's the first iteration, the first set # of combinations is the same as the set of characters if (empty($combinations)) { $combinations = $chars; } # we're done if we're at size 1 if ($size == 1) { return $combinations; } # initialise array to put new values in $new_combinations = array(); # loop through existing combinations and character set to create strings foreach ($combinations as $combination) { foreach ($chars as $char) { $new_combinations[] = $combination . $char; } } # call same function again for the next iteration return sampling($chars, $size - 1, $new_combinations); } // Build vendor combinations as a function of the number of products. Example with 5 products: // ['Amazon', 'Amazon', 'Amazon', 'Newegg', 'Newegg'] $combinations = sampling($vendors, count($products)); foreach ($combinations as $key => $combination) { // Calculate the total price var_dump($combination); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 10, Position 2 = 16
Branch analysis from position: 10
2 jumps found. (Code = 78) Position 1 = 11, Position 2 = 16
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
filename:       /in/AhvWj
function name:  (null)
number of ops:  18
compiled vars:  !0 = $vendors, !1 = $products, !2 = $combinations, !3 = $combination, !4 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
    4     1        ASSIGN                                                   !1, <array>
    5     2        ASSIGN                                                   !2, <array>
   38     3        INIT_FCALL                                               'sampling'
          4        SEND_VAR                                                 !0
          5        COUNT                                            ~8      !1
          6        SEND_VAL                                                 ~8
          7        DO_FCALL                                      0  $9      
          8        ASSIGN                                                   !2, $9
   40     9      > FE_RESET_R                                       $11     !2, ->16
         10    > > FE_FETCH_R                                       ~12     $11, !3, ->16
         11    >   ASSIGN                                                   !4, ~12
   42    12        INIT_FCALL                                               'var_dump'
         13        SEND_VAR                                                 !3
         14        DO_ICALL                                                 
   40    15      > JMP                                                      ->10
         16    >   FE_FREE                                                  $11
   44    17      > RETURN                                                   1

Function sampling:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 6
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 9
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
2 jumps found. (Code = 77) Position 1 = 11, Position 2 = 20
Branch analysis from position: 11
2 jumps found. (Code = 78) Position 1 = 12, Position 2 = 20
Branch analysis from position: 12
2 jumps found. (Code = 77) Position 1 = 13, Position 2 = 18
Branch analysis from position: 13
2 jumps found. (Code = 78) Position 1 = 14, Position 2 = 18
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 18
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
Branch analysis from position: 6
filename:       /in/AhvWj
function name:  sampling
number of ops:  29
compiled vars:  !0 = $chars, !1 = $size, !2 = $combinations, !3 = $new_combinations, !4 = $combination, !5 = $char
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      <array>
   12     3        ISSET_ISEMPTY_CV                                         !2
          4      > JMPZ                                                     ~6, ->6
   13     5    >   ASSIGN                                                   !2, !0
   17     6    >   IS_EQUAL                                                 !1, 1
          7      > JMPZ                                                     ~8, ->9
   18     8    > > RETURN                                                   !2
   22     9    >   ASSIGN                                                   !3, <array>
   25    10      > FE_RESET_R                                       $10     !2, ->20
         11    > > FE_FETCH_R                                               $10, !4, ->20
   26    12    > > FE_RESET_R                                       $11     !0, ->18
         13    > > FE_FETCH_R                                               $11, !5, ->18
   27    14    >   CONCAT                                           ~13     !4, !5
         15        ASSIGN_DIM                                               !3
         16        OP_DATA                                                  ~13
   26    17      > JMP                                                      ->13
         18    >   FE_FREE                                                  $11
   25    19      > JMP                                                      ->11
         20    >   FE_FREE                                                  $10
   32    21        INIT_FCALL_BY_NAME                                       'sampling'
         22        SEND_VAR_EX                                              !0
         23        SUB                                              ~14     !1, 1
         24        SEND_VAL_EX                                              ~14
         25        SEND_VAR_EX                                              !3
         26        DO_FCALL                                      0  $15     
         27      > RETURN                                                   $15
   34    28*     > RETURN                                                   null

End of function sampling

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
151.7 ms | 1394 KiB | 16 Q