phpstringsequenceincrementalphanumeric

Generating Alphanumeric Codes in Sequence from AAA000 to ZZZ999


I needed to create a sequence of unique alphanumeric codes in PHP, starting from 'AAA000' and incrementing up to 'ZZZ999'. The codes should follow this pattern:

  • From 'AAA000' to 'AAA999', then 'AAB000' to 'AAB999', and so on.
  • After 'AAZ999', it should continue with 'ABA000' instead of 'ABZ000'.
  • The final code should be 'ZZZ999'.

This pattern should give a total of 17,576,000 unique codes. I was initially unsure of how to handle the incrementation, especially when rolling over from 'AAZ999' to 'ABA000'. However, I've developed a solution in PHP that seems to work.

My Current Solution:

for($letter_code = 'aaa'; $letter_code < 'zzz'; $letter_code++){
    if($letter_code != 'zzy'){
        for($number_code = 0; $number_code <= 999; $number_code++){
            $code_str_number = str_pad($number_code, 3, "0", STR_PAD_LEFT);
            $total_records_processed_count++;
            print (strtoupper($letter_code).$code_str_number."\n");
        }
    } else {
        // Handle 'ZZZ' after 'ZZY'
        for($number_code = 0; $number_code <= 999; $number_code++){
            $code_str_number = str_pad($number_code, 3, "0", STR_PAD_LEFT);
            $total_records_processed_count++;
            print (strtoupper($letter_code).$code_str_number."\n");
        }
        for($number_code = 0; $number_code <= 999; $number_code++){
            $code_str_number = str_pad($number_code, 3, "0", STR_PAD_LEFT);
            $total_records_processed_count++;
            print ("ZZZ".$code_str_number."\n");
        }
    }
}

Explanation:
I used a nested loop, the outer for the alphabetic part and the inner for the numeric part. To handle the numeric part, I used str_pad to ensure the number always has three digits. The special case for when $letter_code reaches 'zzy' is handled to include 'ZZZ'.

Question:
While this code works, I'm interested in knowing if there's a more efficient or elegant way to achieve the same result, particularly in handling the special case when the code rolls over from 'ZZY999' to 'ZZZ000'.

If anyone has any idea how I can better this question please leave me a comment and I will make amendments.


Solution

  • First of loop through the letters as they are the beginning of the code.

    Then in your loop for the letters you can run through your numbers and merge the two to create your strings.

    Here is a working solution for this functionality.

    for($letter_code = 'aaa'; $letter_code < 'zzz'; $letter_code++){
        if($letter_code != 'zzy'){
    // Reason for the if != 'zzy' is that I couldn't write <= 'zzz'
    // This : <= 'zzz' breaks the loop.
            for($number_code = 0; $number_code <= 999; $number_code++){
                if($number_code > 99){
                    $code_str_number = $number_code;
                }elseif($number_code > 9){
                    $code_str_number = "0".$number_code;
                }else{
                    $code_str_number = "00".$number_code;
                }
                $total_records_processed_count++;
                print (strtoupper($letter_code).$code_str_number."\n");
            }
        }else{
    // Once reaching zzy we loop through zzy and then after do zzz
            for($number_code = 0; $number_code <= 999; $number_code++){
                if($number_code > 99){
                    $code_str_number = $number_code;
                }elseif($number_code > 9){
                    $code_str_number = "0".$number_code;
                }else{
                    $code_str_number = "00".$number_code;
                }
                $total_records_processed_count++;
                print (strtoupper($letter_code).$code_str_number."\n");
            }
            $number_code = 0;
            for($number_code = 0; $number_code <= 999; $number_code++){
                if($number_code > 99){
                    $code_str_number = $number_code;
                }elseif($number_code > 9){
                    $code_str_number = "0".$number_code;
                }else{
                    $code_str_number = "00".$number_code;
                }
                $total_records_processed_count++;
                print ("ZZZ".$code_str_number."\n");
            }
        }
        $number_code = 0;
        // For Testing Purposes just add this if() to cut the full loop 
        // if($letter_code == 'aab'){
        //  die();
        // }
    }