Arrange an array by the first letter

Long time, no post :)
Here is a small PHP tips&tricks post : we have an array full of values, let’s say a country list (in this example the list is limited, we don’t want to see 190+ values).

  1. (
  2.     [0] => Array
  3.         (
  4.             [0] => Afghanistan
  5.             [name] => Afghanistan
  6.             [1] => AF
  7.             [code] => AF
  8.         )
  9.  
  10.     [1] => Array
  11.         (
  12.             [0] => Albania
  13.             [name] => Albania
  14.             [1] => AL
  15.             [code] => AL
  16.         )
  17.  
  18.     [2] => Array
  19.         (
  20.             [0] => Algeria
  21.             [name] => Algeria
  22.             [1] => DZ
  23.             [code] => DZ
  24.         )
  25.  
  26.  
  27.     [3] => Array
  28.         (
  29.             [0] => Bangladesh
  30.             [name] => Bangladesh
  31.             [1] => BD
  32.             [code] => BD
  33.         )
  34.  
  35.     [4] => Array
  36.         (
  37.             [0] => Barbados
  38.             [name] => Barbados
  39.             [1] => BB
  40.             [code] => BB
  41.         )
  42.  
  43.    
  44.     [5] => Array
  45.         (
  46.             [0] => Belgium
  47.             [name] => Belgium
  48.             [1] => BE
  49.             [code] => BE
  50.         )
  51.  
  52.  
  53.     [6] => Array
  54.         (
  55.             [0] => Brazil
  56.             [name] => Brazil
  57.             [1] => BR
  58.             [code] => BR
  59.         )
  60.  
  61.  
  62.     [7] => Array
  63.         (
  64.             [0] => Cape Verde
  65.             [name] => Cape Verde
  66.             [1] => CV
  67.             [code] => CV
  68.         )
  69.  
  70.     [8] => Array
  71.         (
  72.             [0] => Cayman Islands
  73.             [name] => Cayman Islands
  74.             [1] => KY
  75.             [code] => KY
  76.         )
  77. )

and we want to display like this

-A-
  • Afghanistan
  • Albania
  • Algeria
-B-
  • Bangladesh
  • Barbados
  • Belgium
  • Brazil
-C-
  • Cape Verde
  • Cayman Islands

, the right and easy way to do this would be to retain in a variable the first letter of the last country and in another variable the first letter of the current country. We compare them and if they are different we just output the first letter and then the name of the current country. All this in just few and simple to understand lines, right? :P

"; } print $v[$i]["name"] . "
"; $last_letter = $current_letter; }
  1. //try to do the A-Z list
  2. $v = function to get your country list from a DB sorted by country name!;
  3. $last_letter = '';
  4. $current_letter = '';
  5. for ($i=0;$i<count($v);$i++)
  6. {
  7.         $current_letter = substr($v[$i]["name"],0,1);
  8.         if ($last_letter != $current_letter)
  9.         {
  10.                 print "<div align=center>" . $current_letter . "</div>";
  11.         }
  12.         print $v[$i]["name"] . "<br />";
  13.         $last_letter = $current_letter;
  14. }

I hope this will help someone ;)

Revision
09.12.2007, 12:09 : added "sorted by country name!" comment in the php code example. Thanks to Dragos.