Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Aaron Walton
3,557 PointsWebsite for PHP array data downloads
Does anyone know of some good sites that has common data prepared in php arrays that developers can use? For example, an array that contains all 50 states in the US (I've been able to find this one). What I need is an array or arrays that contain counties by US state.
1 Answer
Riley Hilliard
Courses Plus Student 17,771 PointsYou can hit this API to return counties by state. Here is an example JSON response for all counties in California via PHP:
$county_data = json_decode(file_get_contents("http://api.sba.gov/geodata/city_county_links_for_state_of/ca.json"));
// Sample echo of data on Marina:
echo $county_data[0]->name." is located in ".$county_data[0]->full_county_name.", which is in the state of ".$county_data[0]->state_name.", and is located at ".$county_data[0]->primary_latitude." latitude and ".$county_data[0]->primary_longitude." longitude.";
// Will write "Marina is located in Monterey County, which is in the state of California, and is located at 36.68 latitude and -121.8 longitude."
Aaron Walton
3,557 PointsAaron Walton
3,557 PointsThanks for that. I've been playing with this but i'm not sure how to get to a list of counties. THe $county_data array has 489 elements whereas CA only has 58 counties so I need to figure out how to simplify this output into and array of counties.
Riley Hilliard
Courses Plus Student 17,771 PointsRiley Hilliard
Courses Plus Student 17,771 PointsThat was just an example I made based off one of the potential API calls. That happens to be calling data for major cities/towns in California (There happen to be 489)
If you want data on every county in California the API call URL is:
http://api.sba.gov/geodata/county_links_for_state_of/ca.jsonAaron Walton
3,557 PointsAaron Walton
3,557 PointsGreat thanks!