在PHP中迭代此JSON字符串

时间:2017-04-06 02:21:20

标签: php arrays json

这是我的档案,名为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"
        }
    ]
}

这是我的PHP嵌入在html文件中调用它:

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

我不知道如何重复遍历我的数组。当然,我理论上将把所有50个州都输入这里。

我已经阅读了其他帖子,询问这个并且他们的方法不起作用,因为我的JSON格式总是与它们看起来不同!

1 个答案:

答案 0 :(得分:2)

我会想到一个非常简单的循环会做到这一点

foreach ($parksData["state"] as $state)
{
    echo $state["name"];
}