PHP - loop through letters

前端 未结 3 818
名媛妹妹
名媛妹妹 2021-02-18 16:54

I am trying to loop through letters rather than numbers.

I am trying to do this using chr and the number equivalent but it doesn\'t seem to be happening!

I want

相关标签:
3条回答
  • 2021-02-18 17:06
    for( $x = "AAAA"; ; $x++) {
        echo $x."\n";
        if( $x == "ZZZZ") break;
    }
    

    Incrementing a letter will cycle it through the alphabet similar to the column names in Excel.

    0 讨论(0)
  • 2021-02-18 17:14

    Another way to solve this

    $i = 'AAAA';
    do {
      echo $i . "\n";
      $i++;
    } while( $i !== 'AAAAA');
    
    0 讨论(0)
  • 2021-02-18 17:24

    Why don't you make an array of letters and then use nested loops:

    $letters = range('A', 'Z');
    
    foreach ($letters as $one) {
      foreach ($letters as $two) {
        foreach ($letters as $three) {
          foreach ($letters as $four) {
            echo "$one$two$three$four";
          }
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题