US State Names & Abbreviations as PHP Array

I needed a PHP array of US State Names & Abbreviations in a project the other day. Here’s four arrays: An entire list of US abbreviations, US State Names, and two associative arrays. Information pulled directly from USPS.Hope it helps!

In

4 responses

  1. Good ref. to USPS, totally forgot about them!

    Why do you have so many arrays vs. just one?

    Here’s method I use: https://gist.github.com/gists/3176437/ to go from “Maryland” to “md”. Haven’t added support yet to go from “md” to “Maryland”.

    1. I only used one array for the project I was working with, but figured someone might want / need it in a different format and didn’t want to mess with flipping the array, etc. Your function makes a bit more sense than a whole bunch of arrays though 🙂 Thanks!

      1. I don’t think “mess” was the right word choice there. 😉

        You need only a single array: $us_state_abbrevs_names. Then use array_flip() or array_keys() if needed. Having multiple arrays with the same contents is a lot messier and less DRY. (Gist mentioned above seems to be gone, by the way.)

        1. Heh yeah you’re absolutely right — I almost don’t want to look at my code from 6 month ago because of stuff like this! But at least I can re-write history and change the gist 🙂