1

I have to generate a list of all possible combinations of numbers and letters of length three. The catch is on the first two characters can be letters or numbers, and the third one can only be numeric.

For example:

AA1, AA2, AA3 .... FC7 ... 001, 002 ... 365)

I hope you can all help me. I look forward to these responses. Regards, Josh.

So far i only managed a very simple way to get all number with prevailing zeros

for ($k = 0 ; $k < 999; $k++) {
     $rnd[] = sprintf('%03d',$k);
}
Mark Cidade
  • 98,437
  • 31
  • 224
  • 236
OniJoshin
  • 325
  • 1
  • 4
  • 20

2 Answers2

2

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

Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • 1
    Thank you for your answer but I literally just found a solution as i posted above. Thanks anyway :) greatly appreciated. I marked yours as the correct answer due to good information and easier understanding with good variable names. – OniJoshin May 19 '15 at 17:51
  • @jburley89 updated my answer; had a few typos in it and changed the letters to uppercase letters only. – Rizier123 May 19 '15 at 18:13
1

The following code worked perfectly for what i was after.

$letters = range('A', 'Z');
$comb = array();
for ($k = 0 ; $k <= 9; $k++) {
    foreach($letters as $l){
        foreach($letters as $le){
            $comb[]= $l.$le.$k;
        }
    }
}
for ($k = 0 ; $k <= 999; $k++) {
    $comb[] = sprintf('%03d',$k);
}
OniJoshin
  • 325
  • 1
  • 4
  • 20
  • 1
    Your solution is not complete. You're *missing like 5000 results*, look for `C49` for example. You can discard the last loop (the one using `sprintf`) and you have to expand your range: `$letters = array_merge( range('A', 'Z'), range(0, 9) );`. – insertusernamehere May 19 '15 at 18:18
  • 1
    @insertusernamehere He's missing 5'200 combinations, which is around half of all combinations (12'960) :) – Rizier123 May 19 '15 at 18:36
  • Agreed there is a lot missing here. Thanks for pointing that out. I have accepted the correct answer. – OniJoshin May 19 '15 at 20:21
  • @Rizier123 Haha, I didn't do the actual math - it was just a rough estimate. Still close though. :D – insertusernamehere May 19 '15 at 20:36