【问题标题】:How to list from A to Z in PHP, and then on to AA, AB, AC, etc [duplicate]如何在PHP中从A到Z列出,然后到AA,AB,AC等[重复]
【发布时间】:2014-09-21 11:03:48
【问题描述】:

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

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

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

我在 Google 上进行了快速搜索,但没有找到任何东西,但我想方法会有所不同。

我想我可以通过使用 for 循环和一个包含字母的数组来做到这一点,尽管这种方式看起来有点不礼貌。

还有其他方法吗?

谢谢。

【问题讨论】:

  • 使用您的代码示例作为胚胎的递归函数怎么样?

标签: php


【解决方案1】:

PHP 有一个字符串增量操作符,可以做到这一点:

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 转换为 '['('Z' 的 ASCII 值是 90,'[' 的 ASCII 值是 91)。请注意,字符变量可以递增但不能递减,即使如此,也仅支持纯 ASCII 字母和数字(a-z、A-Z 和 0-9)。其他字符变量自增/自减无效,原字符串不变。

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

【讨论】:

  • 不得不说,它在上述情况下工作得很好,但是当你尝试使用 for ($x = 'A'; $x &lt; 'AZ'; $x++) 时,它会中断。
  • @think123:只需将&lt; 替换为!=
  • 不知道为什么设置&lt;= 不能按预期工作,打印了更多结果。无论如何,如果使用&lt;,我们必须打印最后一个ZZ
  • @KingKing &lt;= 的工作方式与预期完全一致,因为您是在比较按字母顺序(就像在字典中一样),而B之后 字典中的AZ....如果你需要打印最后一个ZZ,那么你比较!== 'AAA'
  • 我没有尝试任何代码,我知道它是如何工作的,我已经多次回答过这个问题,并且几乎每天都在实际代码中使用这种类型的字符递增...... .. 我告诉你不要使用&lt;&lt;=&gt;&gt;= 进行这样的字母比较,但总是 改为使用!==
【解决方案2】:

试试

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

【讨论】:

  • 我想你错过了最后一部分等等?
  • 嗯,有点工作 - 但这怎么会先给我 A、B、C ... Z?
【解决方案3】:

PHP 在处理字符变量的算术运算时遵循 Perl 的约定。

因此可以在 php 中增加字母表

$limit = "AZ";

for($x = "A", $limit++; $x != $limit; $x++) {

    echo "$x ";

}

会给你结果

A
B
C
.
.
.
AX
AY
AZ

希望这会有所帮助。

【讨论】:

    【解决方案4】:

    我做了一个常数时间函数如下

    此函数给出数字索引的字母表示

    public static $alpha = ['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'];
    
    public static function getColName($index){
      $index--;
      $nAlphabets = 26;
      $f = floor($index/pow($nAlphabets,0)) % $nAlphabets;
      $s = (floor($index/pow($nAlphabets,1)) % $nAlphabets)-1;
      $t = (floor($index/pow($nAlphabets,2)) % $nAlphabets)-1;
    
      $f = $f < 0 ? '' : self::$alpha[$f];
      $s = $s < 0 ? '' : self::$alpha[$s];
      $t = $t < 0 ? '' : self::$alpha[$t];
    
      return trim("{$t}{$s}{$f}");
    
    }
    

    现在,如果您想使用它,请创建一个范围。您可以在循环中调用此函数,将您的值推送到数组中。

    在大多数情况下,我们需要表示而不是范围,这个函数就可以正常工作。

    如何使用

    只需将这些静态函数封装在一个类中并用作

    className::getColName(47);
    

    在我的例子中创建一个范围是浪费内存。

    适合您的情况

    for($i = 1; $i < 1000; $i++) $range[] = className::getColName($i);
    

    【讨论】:

      【解决方案5】:

      正如@Abhijit Srivastava 所示,但这是一种通用方式。

      function alphaIndex($index) {
          $column = "";
          $nAlphabets = 26;
          $times = (int)($index/$nAlphabets);
          $times = $index%$nAlphabets > 0 ? ($times+1):($times);
          $index--;
      
          for ($i=0; $i < $times; $i++) { 
              $less = $i > 0 ? 1:0;
              $key = (floor($index/pow($nAlphabets,$i)) % $nAlphabets)-$less;
      
              $column = ( $key<0 ? '':chr(65+$key) ).$column;
          }
      
          return $column;
      }
      

      【讨论】:

        【解决方案6】:

        我创建了一个自定义函数,它返回按字母顺序排列的字母范围,指定数量的字母通过(for example: if you set $pass=2, the function returns [A, C, E, ... AA, AC, AE])

        另一个有用的选项是$pairs=true,它将所有字母组合成对(for example: if you set $pairs=true, the function returns a range of consecutive groups like [[A,B],[C,D],[E,F],...[AA,AB],[AC,AD]] for $pass=1 or [[A,C],[D,F],...[AA,AC],[AD,AF]] for $pass=2)

        调用示例:

        $myRange = $this-&gt;AlphaRange('A','AAZ'); // returns all combinations from A to AAZ,

        $myRange = $this-&gt;AlphaRange('A','AAZ',2); // returns consecutive combinations from A to AAZ with letters skiped from 2 to 2,

        $myRange = $this-&gt;AlphaRange('A','AAZ',5,true); // returns consecutive pairs of two letters that contains first and last letter of a group of 5 letters

        希望有用。

            public function AlphaRange($from, $to, $pass=1, $pairs=false) {
                $range = [];
                $currStep = 1;
                $nextStep = $pass+1;
                $currPair = 0;
        
                for($i=$from; $i<'ZZZ'; $i++) {
        
                    if ($currStep == 1) {
                        if (false !== $pairs) {
                            $range[$currPair][] = $i;
                        }
                        else {
                            $range[] = $i;
                        }
                    }
                    else {
                        if ($currStep == $nextStep) {
        
                            if (false !== $pairs) {
        
                                // $range[count($range[$currPair]) == 2 ? ++$currPair : $currPair][] = $i;
        
                                $range[$currPair][] = $lastI; 
                                $range[++$currPair][] = $i;
        
                            }
                            else {
                                $range[]  = $i;
                            }
        
                            $currStep = 1;
                            $nextStep = $pass+1;
                        }
                        else {
                            $lastI = $i;
                        }                           
                    }
        
                    if ($i == $to) {
                        if (false !== $pairs) {
                            if (count($range[$currPair]) == 1) {
                                $range[$currPair][] = $i;
                            }
                        }
                        break;
                    }
        
                    $currStep++;
                }
        
                return $range;
            }
        

        【讨论】:

          猜你喜欢
          • 2015-06-03
          • 1970-01-01
          • 2011-07-30
          • 2016-09-25
          • 1970-01-01
          • 2020-11-09
          • 2013-09-28
          相关资源
          最近更新 更多