You've done the hard work already. Instead of calculating the product, store the selected array.
<?php
$primes = array(2, 3, 5, 7, 11, 13, 17, 19, 23);
$combos = [];
function getAllCombinations($arr, $n, &$combos, $selected = array(), $startIndex = 0) {
if ($n == 0) {
$combos[] = $selected;
// $product = 1;
// foreach ($selected as $prime) {
// $pr[] = $prime;
// $product *= $prime;
// $pr[] = $prime;
// }
// echo "Product: $product\n";
return;
}
for ($i = $startIndex; $i < count($arr); $i++) {
$selected[] = $arr[$i];
getAllCombinations($arr, $n - 1, $combos, $selected, $i + 1);
array_pop($selected); // Backtrack and remove the element for next iteration
}
}
getAllCombinations($primes, 4, $combos);
echo '<pre>';
foreach ($combos as $com) {
printf("%-35s = %5d<br>", join(' × ', $com), array_product($com)); // output numbers and product
}
?>
giving
2 × 3 × 5 × 7 = 210
2 × 3 × 5 × 11 = 330
2 × 3 × 5 × 13 = 390
2 × 3 × 5 × 17 = 510
2 × 3 × 5 × 19 = 570
2 × 3 × 5 × 23 = 690
2 × 3 × 7 × 11 = 462
2 × 3 × 7 × 13 = 546
2 × 3 × 7 × 17 = 714
2 × 3 × 7 × 19 = 798
2 × 3 × 7 × 23 = 966
2 × 3 × 11 × 13 = 858
2 × 3 × 11 × 17 = 1122
2 × 3 × 11 × 19 = 1254
2 × 3 × 11 × 23 = 1518
2 × 3 × 13 × 17 = 1326
2 × 3 × 13 × 19 = 1482
2 × 3 × 13 × 23 = 1794
2 × 3 × 17 × 19 = 1938
2 × 3 × 17 × 23 = 2346
2 × 3 × 19 × 23 = 2622
2 × 5 × 7 × 11 = 770
2 × 5 × 7 × 13 = 910
.
.
5 × 17 × 19 × 23 = 37145
7 × 11 × 13 × 17 = 17017
7 × 11 × 13 × 19 = 19019
7 × 11 × 13 × 23 = 23023
7 × 11 × 17 × 19 = 24871
7 × 11 × 17 × 23 = 30107
7 × 11 × 19 × 23 = 33649
7 × 13 × 17 × 19 = 29393
7 × 13 × 17 × 23 = 35581
7 × 13 × 19 × 23 = 39767
7 × 17 × 19 × 23 = 52003
11 × 13 × 17 × 19 = 46189
11 × 13 × 17 × 23 = 55913
11 × 13 × 19 × 23 = 62491
11 × 17 × 19 × 23 = 81719
13 × 17 × 19 × 23 = 96577