Multidimensional State Array


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. $us_states = array(
  2. array('id' => 1,'abbr' => 'AL', 'name' => 'Alabama'),
  3. array('id' => 2,'abbr' => 'AK', 'name' => 'Alaska'),
  4. array('id' => 3,'abbr' => 'AZ', 'name' => 'Arizona'),
  5. array('id' => 4,'abbr' => 'AR', 'name' => 'Arkansas'),
  6. array('id' => 5,'abbr' => 'CA', 'name' => 'California'),
  7. array('id' => 6,'abbr' => 'CO', 'name' => 'Colorado'),
  8. array('id' => 7,'abbr' => 'CT', 'name' => 'Connecticut'),
  9. array('id' => 8,'abbr' => 'DE', 'name' => 'Delaware'),
  10. array('id' => 9,'abbr' => 'DC', 'name' => 'District of Columbia'),
  11. array('id' => 10,'abbr' => 'FL', 'name' => 'Florida'),
  12. array('id' => 11,'abbr' => 'GA', 'name' => 'Georgia'),
  13. array('id' => 12,'abbr' => 'GU', 'name' => 'Guam'),
  14. array('id' => 13,'abbr' => 'HI', 'name' => 'Hawaii'),
  15. array('id' => 14,'abbr' => 'ID', 'name' => 'Idaho'),
  16. array('id' => 15,'abbr' => 'IL', 'name' => 'Illinois'),
  17. array('id' => 16,'abbr' => 'IN', 'name' => 'Indiana'),
  18. array('id' => 17,'abbr' => 'IA', 'name' => 'Iowa'),
  19. array('id' => 18,'abbr' => 'KS', 'name' => 'Kansas'),
  20. array('id' => 19,'abbr' => 'KY', 'name' => 'Kentucky'),
  21. array('id' => 20,'abbr' => 'LA', 'name' => 'Louisiana'),
  22. array('id' => 21,'abbr' => 'ME', 'name' => 'Maine'),
  23. array('id' => 22,'abbr' => 'MD', 'name' => 'Maryland'),
  24. array('id' => 23,'abbr' => 'MA', 'name' => 'Massachusetts'),
  25. array('id' => 24,'abbr' => 'MI', 'name' => 'Michigan'),
  26. array('id' => 25,'abbr' => 'MN', 'name' => 'Minnesota'),
  27. array('id' => 26,'abbr' => 'MS', 'name' => 'Mississippi'),
  28. array('id' => 27,'abbr' => 'MO', 'name' => 'Missouri'),
  29. array('id' => 28,'abbr' => 'MT', 'name' => 'Montana'),
  30. array('id' => 29,'abbr' => 'NE', 'name' => 'Nebraska'),
  31. array('id' => 30,'abbr' => 'NV', 'name' => 'Nevada'),
  32. array('id' => 31,'abbr' => 'NH', 'name' => 'New Hampshire'),
  33. array('id' => 32,'abbr' => 'NJ', 'name' => 'New Jersey'),
  34. array('id' => 33,'abbr' => 'NM', 'name' => 'New Mexico'),
  35. array('id' => 34,'abbr' => 'NY', 'name' => 'New York'),
  36. array('id' => 35,'abbr' => 'NC', 'name' => 'North Carolina'),
  37. array('id' => 36,'abbr' => 'ND', 'name' => 'North Dakota'),
  38. array('id' => 37,'abbr' => 'OH', 'name' => 'Ohio'),
  39. array('id' => 38,'abbr' => 'OK', 'name' => 'Oklahoma'),
  40. array('id' => 39,'abbr' => 'OR', 'name' => 'Oregon'),
  41. array('id' => 40,'abbr' => 'PA', 'name' => 'Pennsylvania'),
  42. array('id' => 41,'abbr' => 'PR', 'name' => 'Puerto Rico'),
  43. array('id' => 42,'abbr' => 'RI', 'name' => 'Rhode Island'),
  44. array('id' => 43,'abbr' => 'SC', 'name' => 'South Carolina'),
  45. array('id' => 44,'abbr' => 'SD', 'name' => 'South Dakota'),
  46. array('id' => 45,'abbr' => 'TN', 'name' => 'Tennessee'),
  47. array('id' => 46,'abbr' => 'TX', 'name' => 'Texas'),
  48. array('id' => 47,'abbr' => 'UT', 'name' => 'Utah'),
  49. array('id' => 48,'abbr' => 'VT', 'name' => 'Vermont'),
  50. array('id' => 49,'abbr' => 'VA', 'name' => 'Virginia'),
  51. array('id' => 50,'abbr' => 'VI', 'name' => 'Virgin Islands'),
  52. array('id' => 51,'abbr' => 'WA', 'name' => 'Washington'),
  53. array('id' => 52,'abbr' => 'WV', 'name' => 'West Virginia'),
  54. array('id' => 53,'abbr' => 'WI', 'name' => 'Wisconsin'),
  55. array('id' => 54,'abbr' => 'WY', 'name' => 'Wyoming')
  56. );
  57.  
  58. function getStateNameByID($id)
  59. {
  60. foreach ($us_states as $state) {
  61. if ($state["id"] == $id) {
  62. return $state["name"];
  63. }
  64. }
  65. }
  66.  
  67. function getStateAbbrByID($id)
  68. {
  69. foreach ($us_states as $state) {
  70. if ($state["id"] == $id) {
  71. return $state["abbr"];
  72. }
  73. }
  74. }
  75.  
  76. function getStateIdByAbbr($abbr)
  77. {
  78. foreach ($us_states as $state) {
  79. if ($state["abbr"] == $abbr) {
  80. return $state["id"];
  81. }
  82. }
  83. }
  84.  
  85. function getStateIdByName($name)
  86. {
  87. foreach ($us_states as $state) {
  88. if ($state["name"] == $name) {
  89. return $state["id"];
  90. }
  91. }
  92. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.