dongshen4129 2015-05-29 07:30
浏览 80
已采纳

具有逻辑顺序的字母的数字

I have this array which links numbers to letters at the moment like this:

1-26 = A-Z

But there is more, 27=AA and 28=AB etc...

so basically when I do this:

var_dump($array[2]); //shows B
var_dump($array[29]); //shows AC

Now this array I made myself but it's becoming way too long. Is there a way to actually get this going on till lets say 32? I know there is chr but I dont think I can use this.

Is there an easier way to actually get this without using this way too long of an array?

  • 写回答

3条回答 默认 最新

  • dqd82461 2015-05-29 07:34
    关注

    It's slower calculating it this way, but you can take advantage of the fact that PHP lets you increment letters in the same way as numbers, Perl style:

    function excelColumnRange($number) {
        $character = 'A';
        while ($number > 1) {
            ++$character;
            --$number;
        }
        return $character;
    }
    
    var_dump(excelColumnRange(2));
    var_dump(excelColumnRange(29));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计