简体   繁体   English

生成字母系列PHP

[英]Generate alphabetic series PHP

I want to create alphabetic series in php. 我想在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. 我已经经历过range function并使用ascii值来打印字母,但是仍然找不到创建此类系列的好方法。

You can use this coding: 您可以使用以下编码:

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

You can also use while . 您也可以使用while PHP can increment characters!! PHP可以增加字符!!

$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] =>
  [...]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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