1

This is my file, titled parks.JSON:

{
    "state": [
        {
            "name": "Alabama",
            "park1": "Bladon Springs State Park",
            "park1Link": "http://www.stateparks.com/bladon_springs_state_park_in_alabama.html",
            "park2": "Florala State Park",
            "park2Link": "http://www.stateparks.com/florala_state_park_in_alabama.html"
        },
        {
            "name": "Alaska",
            "park1": "Chugach State Park",
            "park1Link": "http://www.stateparks.com/chugach_state_park_in_alaska.html",
            "park2": "Kachemak Bay State Park",
            "park2Link": "http://www.stateparks.com/kachemak_bay_state_park_in_alaska.html"
        }
    ]
}

And this is my php embedded in an html file to call it:

$json_url = "../data/parks.JSON";
$parksJSON = file_get_contents($json_url);
$parksData = json_decode($parksJSON, TRUE);

I am not sure how to go about iterating through my array. I, of course, will have all 50 states entered here in theory.

I have read other posts asking this and their methods don't work because my JSON format is always different from theirs it seems!

Wynx
  • 27
  • 8
  • 4
    Possible duplicate of [How do I extract data from JSON with PHP?](http://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – user3942918 Apr 06 '17 at 02:22
  • The instructions on that link would have me write this: echo 'State: '.$parksData->state[1]->name; But this displays as "State: " and nothing more. – Wynx Apr 06 '17 at 02:34
  • 1
    Possible duplicate of [Loop through an array php](http://stackoverflow.com/questions/4414623/loop-through-an-array-php) – Matt Apr 06 '17 at 02:38

1 Answers1

2

I would have thought a pretty simple loop would do it

foreach ($parksData["state"] as $state)
{
    echo $state["name"];
}
Trent
  • 2,909
  • 1
  • 31
  • 46