tags:

views:

374

answers:

7

I need to loop through an array of data and print an 'incrementing' letter for each array value. I know I can do this:

$array = array(11, 33, 44, 98, 1, 3, 2, 9, 66, 21, 45); // array to loop through
$letters = array('a', 'b', 'c', ...); // array of letters to access
$i = 0;
foreach($array as $value) {
    echo $letters[$i++] . " - $value";
}

It seems that there should be a better way than having to create an alphabet array. Any suggestions?

Note - My loop will never get through the entire alphabet, so I'm not concerned about running out of letters.

+23  A: 

Use the range function:

$letters = range('a', 'z');
print_r($letters);

You can also use foreach loop to take on each letter individually:

foreach($letters as $letter) {
    echo $letter . '<br />';
}
Sarfraz
Cool. I didn't know range() accepted letters.
Scott Saunders
@Scott Saunders: You are welcome.......(
Sarfraz
I foresee a Populist badge in your future
Michael Mrozek
You're welcome :) I accepted the other answer because your loop is not appropriate for my question.
Scott Saunders
Probably `foreach($letters as $theLetter) {` is slightly simpler to use, unless you need the key for any reason.
Peter Ajtai
@Peter: perhaps, but I'm actually looping through an entirely different array - not the letters array.
Scott Saunders
@Scott - I see what you mean. Take a look at my answer.
Peter Ajtai
haha sorry dude...but anyways, you only beat me by about 20 seconds..and actually i woulda gotten it in first but i got hit with the I'm a human! captcha thing when I first hit submit! Cheer up, I upvoted you :P
Crayon Violent
@crayon I don't think that's the issue. Rather, his answer was a bit more developed. (Granted, just a small bit more.) I dunno if the OP realizes, but the `foreach` loop originally provided in the answer would give him a counter in `$key` that he wouldn't need to increment manually, since the the loop was processing a numeric array.
George Marian
+15  A: 
$letters = range('a','z');
Crayon Violent
+3  A: 
for ($counter = ord('a'); $counter <= ord('z'); $counter += 1) {
    echo chr($counter) . " - $counter";
}
JohnFx
Ewww. There are far cleaner ways than to loop between two magic-looking numbers.
cHao
+1 for the only answer so far that doesn't require a throwaway array of letters.
Scott Saunders
@CHao - Changed it to remove magic numbers, better?
JohnFx
Better. The range('a', 'z') solutions still feel more elegant, but this does at least keep from creating the extra array. The echo needs changing, though, as $value was being set by the foreach loop -- and the numbers need to be echoed too somehow.
cHao
+3  A: 

Did you mean to have something that looked like this?

foreach(range('a','z') as $value)
{
  echo $value . ","
}
Thomas Langston
Crayon's mention of the range function indicated a better way. Code example updated to reflect.
Thomas Langston
A: 

You won't have to have an iterator if you make use of the $key => $value functionality of a foreach loop:

$letters = range('a', 'z');

foreach($letters as $key => $value)
{
    echo $key . '=>' . $value;
}

You could even go as far as to simply do:

foreach(range('a', 'z') as $key => $value)
{
    echo $key . '=>' . $value;
}
evolve
+3  A: 

Just as a demonstration (I know you've already accepted an answer), but it's sometimes useful to know that you can also increment character variables:

$var = 'a';
do {
   echo $var++.'<br />';
} while ($var != 'aa');
Mark Baker
+2  A: 

I realize you already accepted and answer, but I believe this is what you're looking for:

Simple use of the increment operator:

<?php
$array = array("cat","car","far","tar","tag");

  // No need for an array, just set $letter to "a", then increment it.
$letter = "a";

foreach($array as $value) {
      // Print your letter, then increment it.
    echo $letter++ . " - $value\n";
}
?>

Look at it live.

The big advantage of using this method over creating an array is that you don't need to worry about running out of letters, since you just move into double letters: like this.

Peter Ajtai