0

I am using an array for selecting the column in Excel. Right now I need A through CZ, but it might need to expand to a larger set in the future. I'm hoping that someone has a faster or better way than what I'm currently doing this.

Here is what I'm doing now:

$ColumnArray2 = array('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');
$ColumnArray = array();
for($x = 0 ; $x <= 25 ; $x++)
{
    $ColumnArray[] = $ColumnArray2[$x];
}
for($x = 0 ; $x <= 4 ; $x++)
{
    for($y = 0 ; $y <= 25 ; $y++)
    {
        $ColumnArray[] = $ColumnArray2[$x].$ColumnArray2[$y];
    }
}

This gives me an array with 155 values going from A to EZ.

Is there a better way to accomplish this?

Mike
  • 1,853
  • 3
  • 45
  • 75

2 Answers2

2

You do realise that PHP supports perl-style incrementing of characters, which means that you can do:

$startColumn = 'A';
$endColumn = 'CZ';
++$endColumn;

$ColumnArray = [];
for($column = $startColumn; $column != $endColumn; ++$column) {
    $ColumnArray[] = $column;
}

The only thing to watch out for is the end condition cannot use < or >, so we increment the actual end, and use !=

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • I did not know that PHP supports Perl-style incrementing. I don't know Perl anyway so that wouldn't have come up for me. Thaks for a simple answer though. – Mike Aug 15 '17 at 13:29
0

One of solutions:

$ColumnArray2 = array('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');
$ColumnArray = array();
for($x = 0 ; $x <= 5 ; $x++)
{
    foreach ($ColumnArray2 as $v) {
        $ColumnArray[] = $x == 0? $v : $ColumnArray[$x-1] . $v;
    }
}
u_mulder
  • 54,101
  • 5
  • 48
  • 64