11

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.

5
  • 1
    Thank you all for your answers. One thing it seems I didn't make very clear: I'm not looping through the letters, I'm looping through a different array of data. I just need incrementing letters as I go through that data. I'm just noting this in explanation for anyone visiting in the future. You've all provided me with the answers I need. Thanks again. Aug 4, 2010 at 19:20
  • 1
    Scott, this comment should really be folded into the original question as an update, where it will be more prominent. Aug 5, 2010 at 1:02
  • @George: I thought it was pretty clear from the code sample I provided and the original description. Obviously I was wrong :) I've edited the question a bit to make it more clear. Aug 5, 2010 at 13:12
  • I see now that my question title was very misleading. I think the text of the question described what I wanted, but the title described a different problem - the one everyone answered. I've changed the title to reflect my real question. Aug 5, 2010 at 13:15
  • I know what you mean, but we need to work with the human brain's capabilities and all that jazz. I missed the $array in the foreach on my first read. Then, there was the question of whether it's a typo or not. And, as you mention, the title was a bit different than the description. Either way, my point was simply to place that comment in the body of the question, as it is easy to overlook in the comments section. If it's important enough to the question, might as well edit and not worry about changing the question. Or just add it as an addendum, without rewording the original. Aug 5, 2010 at 17:36

8 Answers 8

23

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 />';
}
8
  • 5
    I foresee a Populist badge in your future Aug 4, 2010 at 21:22
  • You're welcome :) I accepted the other answer because your loop is not appropriate for my question. Aug 4, 2010 at 22:07
  • Probably foreach($letters as $theLetter) { is slightly simpler to use, unless you need the key for any reason. Aug 5, 2010 at 0:20
  • @Peter: perhaps, but I'm actually looping through an entirely different array - not the letters array. Aug 5, 2010 at 0:28
  • 2
    @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. Aug 5, 2010 at 17:45
20
$letters = range('a','z');
8

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');
4

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.

3

Did you mean to have something that looked like this?

foreach(range('a','z') as $value)
{
  echo $value . ","
}
1
  • Crayon's mention of the range function indicated a better way. Code example updated to reflect. Aug 4, 2010 at 18:09
3
for ($counter = ord('a'); $counter <= ord('z'); $counter += 1) {
    echo chr($counter) . " - $counter";
}
3
  • Ewww. There are far cleaner ways than to loop between two magic-looking numbers.
    – cHao
    Aug 4, 2010 at 18:18
  • +1 for the only answer so far that doesn't require a throwaway array of letters. Aug 4, 2010 at 18:34
  • 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
    Aug 4, 2010 at 19:04
0

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;
}
0

To add for fun...

$array = array(11, 33, 44, 98, 1, 3, 2, 9, 66, 21, 45);
$new_array = array_combine(array_slice(range('a','z'),0,count($array)),$array);
    foreach($new_array as $k=>$v){
        echo "$k - $v";
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.