I am looking for an algorithm to generate an array of all possible 5 digit combinations of numbers in range 2-9 while having no repeating digits in combinations. For example: 23456 37895 62784 96742 83467 ...
What i don't want to see is for example: 22456 34463 44444 78772 ...
I have got code, which generates all possible combinations, but having repeating numbers in combinations. So maybe that array could be filtered instead having only combinations which contain unique digits.
function sampling($chars, $size, $combinations = array()) {
$charsArray = str_split($chars);
# if it's the first iteration, the first set
# of combinations is the same as the set of characters
if (empty($combinations)) {
$combinations = $charsArray;
}
# 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 ($charsArray as $char) {
$new_combinations[] = $combination . $char;
}
}
# call same function again for the next iteration
return sampling($chars, $size - 1, $new_combinations);
}
// example
$output = sampling("23456789", 5);
print "<pre>";print_r($output);