简体   繁体   中英

Generate alphabetic series PHP

I want to create alphabetic series in php. The series I want to create is something like:

AAAA
AAAB
AAAC
.
.
.
.
.
.
.
ZZZY
ZZZZ

I do not want any numbers. Pure alphabetic series. What approach can I use to create this? Any hints or pseudo code would be helpful.

I have already gone through range function and using ascii values to print alphabets but still could not find a good approach to create such a series.

You can use this coding:

<?php
for ($char = 'AAAA'; $char <= 'Z'; $char++) {
    echo $char . "\n";
}
?>

You can also use while . PHP can increment characters!!

$letter = 'AAAA';
while (($letters[] = $letter++) !== 'ZZZZ') {}
print_r($letters);

Four nested loops iterating over the set of all valid characters. Then all you have to do is concatenate the current character of each loop to a string:

<?php
$chars = range('A', 'Z');
$words = [];
foreach ($chars as $char1) {
  foreach ($chars as $char2) {
    foreach ($chars as $char3) {
      foreach ($chars as $char4) {
        $words[] = $char1 . $char2 . $char3 . $char4;
      }
    }
  }
}
var_dump($words);

The output obviously is:

array(456976) {
  [0] =>
  string(4) "AAAA"
  [1] =>
  string(4) "AAAB"
  [2] =>
  string(4) "AAAC"
  [3] =>
  string(4) "AAAD"
  [4] =>
  string(4) "AAAE"
  [5] =>
  [...]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM