- array_push: documentation ( source)
- array_merge: documentation ( source)
- var_export: documentation ( source)
- array_product: documentation ( source)
<?php
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));
This script was stopped while abusing our resources