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!

2
  • The instructions on that link would have me write this: echo 'State: '.$parksData->state[1]->name; But this displays as "State: " and nothing more. Commented Apr 6, 2017 at 2:34
  • 1
    Possible duplicate of Loop through an array php Commented Apr 6, 2017 at 2:38

1 Answer 1

2

I would have thought a pretty simple loop would do it

foreach ($parksData["state"] as $state)
{
    echo $state["name"];
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.