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 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助