像数字一样在php中按数字递增字母

像数字一样在php中按数字递增字母

问题描述:

如果我写的话,在php中

In php if I writ

$c='A';
$c++;
它增加到"B",但如果我想将其增加2,3或更多, 例如:$ c + 2或$ c + 3,以获取备用字母

$c='A';
$c++;
it increments to 'B' but if I want to increment it with 2 ,3 or more , eg: $c+2 or $c+3 ,to get the alternate alphabets

 for ($column = 'B'; $column < $highestColumn; $column++) {   
 $cell = $worksheet->getCell($column.$row);
 $cell2=$worksheet->getCell($column+1.$row);
 }

但$ column + 1无效

but the $column+1 dont work

该怎么做?

递增字母仅适用于++增量运算符,不适用于+加法.

Incrementing letters will only work with the ++ incrementor operator, not with + addition.

如果要继续使用增量器,可以使用循环:

If you want to continue using an incrementor, you can use a loop:

$column = 'AH';
$step = 7; // number of columns to step by
for($ = 0; $i < $step; $i++) {
    $column++;
}

但是,PHP的字符增量器不会向后工作,因此您不能使用负步长值

However, PHP's character incrementor won't work backwards, so you couldn't use a negative step value

如果要使用加法而不是循环,则需要将该列转换为数值,进行加法,然后再转换回

If you want to use addition rather than a loop, then you need to convert that column to a numeric value, do the addition, then convert back

$column = 'AH';
$step = 7; // number of columns to step by
$columnNumber = PHPExcel_Cell::columnIndexFromString($column) + $step;
$column = PHPExcel_Cell::stringFromColumnIndex($columnNumber - 1);

具有允许您使用负步长值的其他优点

Which has the added benefit of allowing you to use negative step values