skip to Main Content

I have a function that takes randomly take 4 unique numbers from an array and multiply them. Problem with the script below is it only print out the products. I would like for each set of 4 numbers and its product to be in its own array and all the arrays into a main array, forming a 2 dimensional array.

My current script:

//Pruning outcome from controlled lists of Prime Numbers
$primes = array(2, 3, 5, 7, 11, 13, 17, 19, 23);


function getAllCombinations($arr, $n, $selected = array(), $startIndex = 0) {
  if ($n == 0) {
    $product = 1;
    foreach ($selected as $prime) {
      $pr[] = $prime;
      $product *= $prime;
      $pr[] = $prime;
    }
    echo "Product: $productn";
    return;
  }

  for ($i = $startIndex; $i < count($arr); $i++) {
    $selected[] = $arr[$i];
    getAllCombinations($arr, $n - 1, $selected, $i + 1);
    array_pop($selected); // Backtrack and remove the element for next iteration
  }
}  

getAllCombinations($primes, 4);

Instead of using echo resulting as :

Product: 210 Product: 330 Product: 390 Product: 510, 

I would prefer them to be in 2 dimensional array so that each row is the combination of numbers with the product as the last element in the row:

array (
  0 => 
  array (
    0 => 2,
    1 => 3,
    2 => 5,
    3 => 7,
    4 => 210,
  ),
  1 => 
  array (
    0 => 2,
    1 => 3,
    2 => 5,
    3 => 11,
    4 => 330,
  ),
  2 => 
  array (
    0 => 2,
    1 => 3,
    2 => 5,
    3 => 13,
    4 => 390,
  ),
  ...
)

2

Answers


  1. I believe this may be what you are trying to do.

    PHP:

    <?php
    $primes = array(2, 3, 5, 7, 11, 13, 17, 19, 23);
    
    // Create array to hold the combinations and their products
    $allCombinations = [];
    
    function getAllCombinations($arr, $n, &$allCombinations, $selected = array(), $startIndex = 0) {
        if ($n == 0) {
            $product = 1;
            foreach ($selected as $prime) {
                $product *= $prime;
            }
            // Add each combination and its product to the $allCombinations array
            $selected[] = $product;
            $allCombinations[] = $selected;
            return;
        }
    
        for ($i = $startIndex; $i < count($arr); $i++) {
            $selected[] = $arr[$i];
            getAllCombinations($arr, $n - 1, $allCombinations, $selected, $i + 1);
            array_pop($selected);
        }
    }  
    
    getAllCombinations($primes, 4, $allCombinations);
    
    // To view the result
    foreach ($allCombinations as $index => $combination) {
        $textRepresentation = "";
    
        // loop through each element in the combination
        foreach ($combination as $innerIndex => $value) {
            // add the current element's index and value
            if ($innerIndex < count($combination) - 1) { // for elements before the product
                $textRepresentation .= "[$index][$innerIndex] => $value ";
            } else { // for the product
                $textRepresentation .= "and the product would be in [$index][$innerIndex] => $value";
            }
        }
    
        echo $textRepresentation . "<br>";
    }
    
    ?>
    

    OUTPUT:

    [0][0] => 2 [0][1] => 3 [0][2] => 5 [0][3] => 7 and the product would be in [0][4] => 210
    [1][0] => 2 [1][1] => 3 [1][2] => 5 [1][3] => 11 and the product would be in [1][4] => 330
    [2][0] => 2 [2][1] => 3 [2][2] => 5 [2][3] => 13 and the product would be in [2][4] => 390
    [3][0] => 2 [3][1] => 3 [3][2] => 5 [3][3] => 17 and the product would be in [3][4] => 510
    [4][0] => 2 [4][1] => 3 [4][2] => 5 [4][3] => 19 and the product would be in [4][4] => 570
    [5][0] => 2 [5][1] => 3 [5][2] => 5 [5][3] => 23 and the product would be in [5][4] => 690
    [6][0] => 2 [6][1] => 3 [6][2] => 7 [6][3] => 11 and the product would be in [6][4] => 462
    [7][0] => 2 [7][1] => 3 [7][2] => 7 [7][3] => 13 and the product would be in [7][4] => 546
    [8][0] => 2 [8][1] => 3 [8][2] => 7 [8][3] => 17 and the product would be in [8][4] => 714
    [9][0] => 2 [9][1] => 3 [9][2] => 7 [9][3] => 19 and the product would be in [9][4] => 798
    [10][0] => 2 [10][1] => 3 [10][2] => 7 [10][3] => 23 and the product would be in [10][4] => 966
    [11][0] => 2 [11][1] => 3 [11][2] => 11 [11][3] => 13 and the product would be in [11][4] => 858
    [12][0] => 2 [12][1] => 3 [12][2] => 11 [12][3] => 17 and the product would be in [12][4] => 1122
    [13][0] => 2 [13][1] => 3 [13][2] => 11 [13][3] => 19 and the product would be in [13][4] => 1254
    [14][0] => 2 [14][1] => 3 [14][2] => 11 [14][3] => 23 and the product would be in [14][4] => 1518
    [15][0] => 2 [15][1] => 3 [15][2] => 13 [15][3] => 17 and the product would be in [15][4] => 1326
    [16][0] => 2 [16][1] => 3 [16][2] => 13 [16][3] => 19 and the product would be in [16][4] => 1482
    [17][0] => 2 [17][1] => 3 [17][2] => 13 [17][3] => 23 and the product would be in [17][4] => 1794
    [18][0] => 2 [18][1] => 3 [18][2] => 17 [18][3] => 19 and the product would be in [18][4] => 1938
    [19][0] => 2 [19][1] => 3 [19][2] => 17 [19][3] => 23 and the product would be in [19][4] => 2346
    [20][0] => 2 [20][1] => 3 [20][2] => 19 [20][3] => 23 and the product would be in [20][4] => 2622
    [21][0] => 2 [21][1] => 5 [21][2] => 7 [21][3] => 11 and the product would be in [21][4] => 770
    [22][0] => 2 [22][1] => 5 [22][2] => 7 [22][3] => 13 and the product would be in [22][4] => 910
    [23][0] => 2 [23][1] => 5 [23][2] => 7 [23][3] => 17 and the product would be in [23][4] => 1190
    [24][0] => 2 [24][1] => 5 [24][2] => 7 [24][3] => 19 and the product would be in [24][4] => 1330
    [25][0] => 2 [25][1] => 5 [25][2] => 7 [25][3] => 23 and the product would be in [25][4] => 1610
    [26][0] => 2 [26][1] => 5 [26][2] => 11 [26][3] => 13 and the product would be in [26][4] => 1430
    [27][0] => 2 [27][1] => 5 [27][2] => 11 [27][3] => 17 and the product would be in [27][4] => 1870
    [28][0] => 2 [28][1] => 5 [28][2] => 11 [28][3] => 19 and the product would be in [28][4] => 2090
    [29][0] => 2 [29][1] => 5 [29][2] => 11 [29][3] => 23 and the product would be in [29][4] => 2530
    [30][0] => 2 [30][1] => 5 [30][2] => 13 [30][3] => 17 and the product would be in [30][4] => 2210
    [31][0] => 2 [31][1] => 5 [31][2] => 13 [31][3] => 19 and the product would be in [31][4] => 2470
    [32][0] => 2 [32][1] => 5 [32][2] => 13 [32][3] => 23 and the product would be in [32][4] => 2990
    [33][0] => 2 [33][1] => 5 [33][2] => 17 [33][3] => 19 and the product would be in [33][4] => 3230
    [34][0] => 2 [34][1] => 5 [34][2] => 17 [34][3] => 23 and the product would be in [34][4] => 3910
    [35][0] => 2 [35][1] => 5 [35][2] => 19 [35][3] => 23 and the product would be in [35][4] => 4370
    [36][0] => 2 [36][1] => 7 [36][2] => 11 [36][3] => 13 and the product would be in [36][4] => 2002
    [37][0] => 2 [37][1] => 7 [37][2] => 11 [37][3] => 17 and the product would be in [37][4] => 2618
    [38][0] => 2 [38][1] => 7 [38][2] => 11 [38][3] => 19 and the product would be in [38][4] => 2926
    [39][0] => 2 [39][1] => 7 [39][2] => 11 [39][3] => 23 and the product would be in [39][4] => 3542
    [40][0] => 2 [40][1] => 7 [40][2] => 13 [40][3] => 17 and the product would be in [40][4] => 3094
    [41][0] => 2 [41][1] => 7 [41][2] => 13 [41][3] => 19 and the product would be in [41][4] => 3458
    [42][0] => 2 [42][1] => 7 [42][2] => 13 [42][3] => 23 and the product would be in [42][4] => 4186
    [43][0] => 2 [43][1] => 7 [43][2] => 17 [43][3] => 19 and the product would be in [43][4] => 4522
    [44][0] => 2 [44][1] => 7 [44][2] => 17 [44][3] => 23 and the product would be in [44][4] => 5474
    [45][0] => 2 [45][1] => 7 [45][2] => 19 [45][3] => 23 and the product would be in [45][4] => 6118
    [46][0] => 2 [46][1] => 11 [46][2] => 13 [46][3] => 17 and the product would be in [46][4] => 4862
    [47][0] => 2 [47][1] => 11 [47][2] => 13 [47][3] => 19 and the product would be in [47][4] => 5434
    [48][0] => 2 [48][1] => 11 [48][2] => 13 [48][3] => 23 and the product would be in [48][4] => 6578
    [49][0] => 2 [49][1] => 11 [49][2] => 17 [49][3] => 19 and the product would be in [49][4] => 7106
    [50][0] => 2 [50][1] => 11 [50][2] => 17 [50][3] => 23 and the product would be in [50][4] => 8602
    [51][0] => 2 [51][1] => 11 [51][2] => 19 [51][3] => 23 and the product would be in [51][4] => 9614
    [52][0] => 2 [52][1] => 13 [52][2] => 17 [52][3] => 19 and the product would be in [52][4] => 8398
    [53][0] => 2 [53][1] => 13 [53][2] => 17 [53][3] => 23 and the product would be in [53][4] => 10166
    [54][0] => 2 [54][1] => 13 [54][2] => 19 [54][3] => 23 and the product would be in [54][4] => 11362
    [55][0] => 2 [55][1] => 17 [55][2] => 19 [55][3] => 23 and the product would be in [55][4] => 14858
    [56][0] => 3 [56][1] => 5 [56][2] => 7 [56][3] => 11 and the product would be in [56][4] => 1155
    [57][0] => 3 [57][1] => 5 [57][2] => 7 [57][3] => 13 and the product would be in [57][4] => 1365
    [58][0] => 3 [58][1] => 5 [58][2] => 7 [58][3] => 17 and the product would be in [58][4] => 1785
    [59][0] => 3 [59][1] => 5 [59][2] => 7 [59][3] => 19 and the product would be in [59][4] => 1995
    [60][0] => 3 [60][1] => 5 [60][2] => 7 [60][3] => 23 and the product would be in [60][4] => 2415
    [61][0] => 3 [61][1] => 5 [61][2] => 11 [61][3] => 13 and the product would be in [61][4] => 2145
    [62][0] => 3 [62][1] => 5 [62][2] => 11 [62][3] => 17 and the product would be in [62][4] => 2805
    [63][0] => 3 [63][1] => 5 [63][2] => 11 [63][3] => 19 and the product would be in [63][4] => 3135
    [64][0] => 3 [64][1] => 5 [64][2] => 11 [64][3] => 23 and the product would be in [64][4] => 3795
    [65][0] => 3 [65][1] => 5 [65][2] => 13 [65][3] => 17 and the product would be in [65][4] => 3315
    [66][0] => 3 [66][1] => 5 [66][2] => 13 [66][3] => 19 and the product would be in [66][4] => 3705
    [67][0] => 3 [67][1] => 5 [67][2] => 13 [67][3] => 23 and the product would be in [67][4] => 4485
    [68][0] => 3 [68][1] => 5 [68][2] => 17 [68][3] => 19 and the product would be in [68][4] => 4845
    [69][0] => 3 [69][1] => 5 [69][2] => 17 [69][3] => 23 and the product would be in [69][4] => 5865
    [70][0] => 3 [70][1] => 5 [70][2] => 19 [70][3] => 23 and the product would be in [70][4] => 6555
    [71][0] => 3 [71][1] => 7 [71][2] => 11 [71][3] => 13 and the product would be in [71][4] => 3003
    [72][0] => 3 [72][1] => 7 [72][2] => 11 [72][3] => 17 and the product would be in [72][4] => 3927
    [73][0] => 3 [73][1] => 7 [73][2] => 11 [73][3] => 19 and the product would be in [73][4] => 4389
    [74][0] => 3 [74][1] => 7 [74][2] => 11 [74][3] => 23 and the product would be in [74][4] => 5313
    [75][0] => 3 [75][1] => 7 [75][2] => 13 [75][3] => 17 and the product would be in [75][4] => 4641
    [76][0] => 3 [76][1] => 7 [76][2] => 13 [76][3] => 19 and the product would be in [76][4] => 5187
    [77][0] => 3 [77][1] => 7 [77][2] => 13 [77][3] => 23 and the product would be in [77][4] => 6279
    [78][0] => 3 [78][1] => 7 [78][2] => 17 [78][3] => 19 and the product would be in [78][4] => 6783
    [79][0] => 3 [79][1] => 7 [79][2] => 17 [79][3] => 23 and the product would be in [79][4] => 8211
    [80][0] => 3 [80][1] => 7 [80][2] => 19 [80][3] => 23 and the product would be in [80][4] => 9177
    [81][0] => 3 [81][1] => 11 [81][2] => 13 [81][3] => 17 and the product would be in [81][4] => 7293
    [82][0] => 3 [82][1] => 11 [82][2] => 13 [82][3] => 19 and the product would be in [82][4] => 8151
    [83][0] => 3 [83][1] => 11 [83][2] => 13 [83][3] => 23 and the product would be in [83][4] => 9867
    [84][0] => 3 [84][1] => 11 [84][2] => 17 [84][3] => 19 and the product would be in [84][4] => 10659
    [85][0] => 3 [85][1] => 11 [85][2] => 17 [85][3] => 23 and the product would be in [85][4] => 12903
    [86][0] => 3 [86][1] => 11 [86][2] => 19 [86][3] => 23 and the product would be in [86][4] => 14421
    [87][0] => 3 [87][1] => 13 [87][2] => 17 [87][3] => 19 and the product would be in [87][4] => 12597
    [88][0] => 3 [88][1] => 13 [88][2] => 17 [88][3] => 23 and the product would be in [88][4] => 15249
    [89][0] => 3 [89][1] => 13 [89][2] => 19 [89][3] => 23 and the product would be in [89][4] => 17043
    [90][0] => 3 [90][1] => 17 [90][2] => 19 [90][3] => 23 and the product would be in [90][4] => 22287
    [91][0] => 5 [91][1] => 7 [91][2] => 11 [91][3] => 13 and the product would be in [91][4] => 5005
    [92][0] => 5 [92][1] => 7 [92][2] => 11 [92][3] => 17 and the product would be in [92][4] => 6545
    [93][0] => 5 [93][1] => 7 [93][2] => 11 [93][3] => 19 and the product would be in [93][4] => 7315
    [94][0] => 5 [94][1] => 7 [94][2] => 11 [94][3] => 23 and the product would be in [94][4] => 8855
    [95][0] => 5 [95][1] => 7 [95][2] => 13 [95][3] => 17 and the product would be in [95][4] => 7735
    [96][0] => 5 [96][1] => 7 [96][2] => 13 [96][3] => 19 and the product would be in [96][4] => 8645
    [97][0] => 5 [97][1] => 7 [97][2] => 13 [97][3] => 23 and the product would be in [97][4] => 10465
    [98][0] => 5 [98][1] => 7 [98][2] => 17 [98][3] => 19 and the product would be in [98][4] => 11305
    [99][0] => 5 [99][1] => 7 [99][2] => 17 [99][3] => 23 and the product would be in [99][4] => 13685
    [100][0] => 5 [100][1] => 7 [100][2] => 19 [100][3] => 23 and the product would be in [100][4] => 15295
    [101][0] => 5 [101][1] => 11 [101][2] => 13 [101][3] => 17 and the product would be in [101][4] => 12155
    [102][0] => 5 [102][1] => 11 [102][2] => 13 [102][3] => 19 and the product would be in [102][4] => 13585
    [103][0] => 5 [103][1] => 11 [103][2] => 13 [103][3] => 23 and the product would be in [103][4] => 16445
    [104][0] => 5 [104][1] => 11 [104][2] => 17 [104][3] => 19 and the product would be in [104][4] => 17765
    [105][0] => 5 [105][1] => 11 [105][2] => 17 [105][3] => 23 and the product would be in [105][4] => 21505
    [106][0] => 5 [106][1] => 11 [106][2] => 19 [106][3] => 23 and the product would be in [106][4] => 24035
    [107][0] => 5 [107][1] => 13 [107][2] => 17 [107][3] => 19 and the product would be in [107][4] => 20995
    [108][0] => 5 [108][1] => 13 [108][2] => 17 [108][3] => 23 and the product would be in [108][4] => 25415
    [109][0] => 5 [109][1] => 13 [109][2] => 19 [109][3] => 23 and the product would be in [109][4] => 28405
    [110][0] => 5 [110][1] => 17 [110][2] => 19 [110][3] => 23 and the product would be in [110][4] => 37145
    [111][0] => 7 [111][1] => 11 [111][2] => 13 [111][3] => 17 and the product would be in [111][4] => 17017
    [112][0] => 7 [112][1] => 11 [112][2] => 13 [112][3] => 19 and the product would be in [112][4] => 19019
    [113][0] => 7 [113][1] => 11 [113][2] => 13 [113][3] => 23 and the product would be in [113][4] => 23023
    [114][0] => 7 [114][1] => 11 [114][2] => 17 [114][3] => 19 and the product would be in [114][4] => 24871
    [115][0] => 7 [115][1] => 11 [115][2] => 17 [115][3] => 23 and the product would be in [115][4] => 30107
    [116][0] => 7 [116][1] => 11 [116][2] => 19 [116][3] => 23 and the product would be in [116][4] => 33649
    [117][0] => 7 [117][1] => 13 [117][2] => 17 [117][3] => 19 and the product would be in [117][4] => 29393
    [118][0] => 7 [118][1] => 13 [118][2] => 17 [118][3] => 23 and the product would be in [118][4] => 35581
    [119][0] => 7 [119][1] => 13 [119][2] => 19 [119][3] => 23 and the product would be in [119][4] => 39767
    [120][0] => 7 [120][1] => 17 [120][2] => 19 [120][3] => 23 and the product would be in [120][4] => 52003
    [121][0] => 11 [121][1] => 13 [121][2] => 17 [121][3] => 19 and the product would be in [121][4] => 46189
    [122][0] => 11 [122][1] => 13 [122][2] => 17 [122][3] => 23 and the product would be in [122][4] => 55913
    [123][0] => 11 [123][1] => 13 [123][2] => 19 [123][3] => 23 and the product would be in [123][4] => 62491
    [124][0] => 11 [124][1] => 17 [124][2] => 19 [124][3] => 23 and the product would be in [124][4] => 81719
    [125][0] => 13 [125][1] => 17 [125][2] => 19 [125][3] => 23 and the product would be in [125][4] => 96577
    
    Login or Signup to reply.
  2. I prefer the elegance of not defining a reference variable to collect all of the results. Instead, I’ve refactored your script to return the result array.

    I’ve used ... (spread operator) to allow the accumulation of payloads without deviating from the desired 2d structure.

    Code: (Demo)

    function getAllCombinations(array $arr, int $n, array $selected = [], int $startIndex = 0): array
    {
        if (!$n) {
            return [array_merge($selected, [array_product($selected)])]; // generate payload
        }
    
        $result = [];
        $count = count($arr);
        for ($i = $startIndex; $i < $count; ++$i) {
            array_push(
                $result,
                ...getAllCombinations($arr, $n - 1, array_merge($selected, [$arr[$i]]), $i + 1)
            );
        }
        return $result;
    }
    
    $primes = [2, 3, 5, 7, 11, 13, 17, 19, 23];
    var_export(getAllCombinations($primes, 4));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search