如何在PHP中从A到Z列出,然后到AA,AB,AC等列出 [英] How to list from A to Z in PHP, and then on to AA, AB, AC, etc

查看:295
本文介绍了如何在PHP中从A到Z列出,然后到AA,AB,AC等列出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何从A到Z列出:

I know how to list from A to Z:

foreach (range('A', 'Z') as $char) {
  echo $char . "\n";
}

但是如何从那里继续列出AA,AB,AC,AD,... AZ,BA,BB,BC等?

But how do I go on from there to list AA, AB, AC, AD, ... AZ, BA, BB, BC and so on?

我做了一个快速的Google搜索,但找不到任何东西,尽管我猜想方法会有所不同.

I did a quick Google search and couldn't find anything, though I guess the approach will be different.

我认为我可以通过使用for循环和内部带有字母的数组来做到这一点,尽管这种方式似乎有点不合理.

I think I can do it by using a for loop and an array with the letters inside, though that way seems a bit uncouth.

还有其他方法吗?

谢谢.

推荐答案

PHP的字符串增量运算符可以做到这一点:

PHP has the string increment operator that does exactly that:

for($x = 'A'; $x < 'ZZ'; $x++)
    echo $x, ' ';

结果:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z AA AB AC AD AE AF... 

参考:

在处理字符变量而非C的算术运算时,PHP遵循Perl的约定.例如,在PHP和Perl中,$ a ='Z'; $ a ++;将$ a转换为"AA",而在C中将a转换为"Z"; a ++;将a变成'['('Z'的ASCII值为90,'['的ASCII值为91).请注意,字符变量可以递增但不能递减,即使如此,也仅支持纯ASCII字母和数字(a-z,A-Z和0-9).递增/递减其他字符变量无效,原始字符串不变.

PHP follows Perl's convention when dealing with arithmetic operations on character variables and not C's. For example, in PHP and Perl $a = 'Z'; $a++; turns $a into 'AA', while in C a = 'Z'; a++; turns a into '[' (ASCII value of 'Z' is 90, ASCII value of '[' is 91). Note that character variables can be incremented but not decremented and even so only plain ASCII alphabets and digits (a-z, A-Z and 0-9) are supported. Incrementing/decrementing other character variables has no effect, the original string is unchanged.

http://php.net/manual/en/language.operators.increment.php

这篇关于如何在PHP中从A到Z列出,然后到AA,AB,AC等列出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆