This should work for you:
Basically I have an array with all letters ([A-Z]
) and an array with all numbers ([0-9]
). Then you define which order of possible characters you want. E.g. here you want letterNumber
, letterNumber
and then the third spot only number
.
After this you loop through everything as many times as characters per combinations which you want(e.g. XXX -> 3 times
). In the loop you go through all combinations which you already have with all characters which you want at this spot.
So after the 1 iteration you have an array with the first character of each combination, which would be: [0-9A-Z]
.
Then in the second iteration you go through all combinations which you already have, here [0-9A-Z]
with the characters which you want on the second spot, here [0-9A-Z]
. So for ever character in the combinations array ([0-9A-Z
]) you get a new combinations with each character of [0-9A-Z]
.
And this repeated over and over until you get your expected combination length.
So at the end you end up with:
letterNumber = 36 = 26 + 10 possible characters ([A-Z0-9])
letter = 26 possible characters ([A-Z])
number = 10 possible characters ([0-9])
36 * 36 * 10 = 12'960 combinations
The code:
<?php
$letters = range("A", "Z");
$numbers = array_merge(range(0, 9));
$order = ["letterNumber", "letterNumber", "number"]; //possibilities: "letter", "number", "letterNumber"
$length = count($order);
$combinations = [[]];
for($count = 0; $count < $length; $count++) {
$tmp = [];
if($order[$count] == "number" || $order[$count] == "letterNumber") {
foreach($combinations as $combination) {
foreach($numbers as $v)
$tmp[] = array_merge($combination, [$v]);
}
}
if($order[$count] == "letter" || $order[$count] == "letterNumber") {
foreach($combinations as $combination) {
foreach($letters as $v)
$tmp[] = array_merge($combination, [$v]);
}
}
$combinations = $tmp;
}
print_r($combinations);
?>
output:
Array
(
[0] => Array
(
[0] => 0
[1] => 0
[2] => 0
)
[1] => Array
(
[0] => 0
[1] => 0
[2] => 1
)
//...
[12959] => Array
(
[0] => Z
[1] => Z
[2] => 9
)
)
Demo