PHP Array Of USA States

23rd April 2009

Use the following array if you want to print out a list of USA states either as a list, or as a select box.

  1. $state_list = array('AL'=>"Alabama",
  2. 'AK'=>"Alaska",
  3. 'AZ'=>"Arizona",
  4. 'AR'=>"Arkansas",
  5. 'CA'=>"California",
  6. 'CO'=>"Colorado",
  7. 'CT'=>"Connecticut",
  8. 'DE'=>"Delaware",
  9. 'DC'=>"District Of Columbia",
  10. 'FL'=>"Florida",
  11. 'GA'=>"Georgia",
  12. 'HI'=>"Hawaii",
  13. 'ID'=>"Idaho",
  14. 'IL'=>"Illinois",
  15. 'IN'=>"Indiana",
  16. 'IA'=>"Iowa",
  17. 'KS'=>"Kansas",
  18. 'KY'=>"Kentucky",
  19. 'LA'=>"Louisiana",
  20. 'ME'=>"Maine",
  21. 'MD'=>"Maryland",
  22. 'MA'=>"Massachusetts",
  23. 'MI'=>"Michigan",
  24. 'MN'=>"Minnesota",
  25. 'MS'=>"Mississippi",
  26. 'MO'=>"Missouri",
  27. 'MT'=>"Montana",
  28. 'NE'=>"Nebraska",
  29. 'NV'=>"Nevada",
  30. 'NH'=>"New Hampshire",
  31. 'NJ'=>"New Jersey",
  32. 'NM'=>"New Mexico",
  33. 'NY'=>"New York",
  34. 'NC'=>"North Carolina",
  35. 'ND'=>"North Dakota",
  36. 'OH'=>"Ohio",
  37. 'OK'=>"Oklahoma",
  38. 'OR'=>"Oregon",
  39. 'PA'=>"Pennsylvania",
  40. 'RI'=>"Rhode Island",
  41. 'SC'=>"South Carolina",
  42. 'SD'=>"South Dakota",
  43. 'TN'=>"Tennessee",
  44. 'TX'=>"Texas",
  45. 'UT'=>"Utah",
  46. 'VT'=>"Vermont",
  47. 'VA'=>"Virginia",
  48. 'WA'=>"Washington",
  49. 'WV'=>"West Virginia",
  50. 'WI'=>"Wisconsin",
  51. 'WY'=>"Wyoming");

If you want to reverse this then use the following code.

  1. $reverse = array();
  2. foreach ( $state_list as $key=>$state ) {
  3. $reverse[$state] = $key;
  4. }
  5. $state_list = $reverse;

Comments

Permalink
Just a note: the correct way to reverse the array would be as follows:
  1. $state_list = array_reverse($state_list,true);
The second argument is important: if you don't include it, the array will be re-indexed with numeric keys instead of the state abbreviations.

Submitted by Justin on Tue, 11/03/2009 - 22:54

Permalink
great post

Submitted by Vedavyas2012 on Sat, 09/11/2010 - 17:46

Add new comment

The content of this field is kept private and will not be shown publicly.