tags:

views:

18

answers:

1

How would I auto generate an array (in PHP) for AA - ZZ and so on like AAA - ZZZ

$column_arr2= range("aa", "zz"); // NOT Working
$row_arr    = range(0,1000);
$column_arr = range("a", "z");

echo "Column2<pre>".print_r($column_arr2, true)."</pre><br />"; // prints a - z
echo "Row<pre>".print_r($row_arr, true)."</pre><br />";
echo "Column<pre>".print_r($column_arr, true)."</pre><br />";

Would like to make the number and alpha arrays dynamic as I'm using this for an excel document.

What I would like is:

$arr = ([0] => 'a', [1] => 'b', [2] => 'c', ...
        [26] => 'aa', [27] => 'ab', [28] => 'ac', ...
        [52] => 'ba', [53] => 'bb', [54] => 'bc', ...
       )

Any ideas are welcome

+3  A: 

PHP supports incrementing strings:

$array = array('A');
$current = 'A';
while ($current != 'ZZZ') {
    $array[] = ++$current;
}
nikic
+1 (since it works). Man PHP has some weird behavior some times. Useful behavior, but not behavior you'd expect...
ircmaxell
Awesome!!! I had no idea PHP supported incrementing strings. One small not is you skip A but if you add it on array creation it works great
Phill Pafford
@ircmaxell - not so weird, because it's a feature inherited/stolen from Perl. What's weird is that you can't decrement strings using --$current;
Mark Baker
@Mark Baker: Fair enough, but I still find it counter-intuitive...
ircmaxell
@Mark: Yeah, I already wondered why they do not support it...
nikic