在PHP中解析JSON以提取值

时间:2016-03-31 19:40:14

标签: php json parsing

我需要在PHP代码中解析JSON。

一个简单的样本......

    $myJSON_string = '{
    "features": [{
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [7.671718, 44.912186]
            },
            "properties": {
                "ca": 183.5372,
                "key": "xyz"
            }
        }, {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [7.685436, 44.921234]
            },
            "properties": {
                "ca": 183.5372,
                "key": "kwh"
            }
        }

    ],
    "type": "FeatureCollection"
}';



    $myJSON_json = json_decode($myJSON_string,true);

    foreach ($myJSON_json as $f) {
        echo $f['features']['properties'][0]['key'].'<br />';
    }

我想提取&#34; key&#34;的值。参数,所以在此示例中

xyz
kwh

我是PHP的新手,很抱歉....任何建议/示例/替代?

提前多多谢谢你!

切萨雷

1 个答案:

答案 0 :(得分:3)

您需要迭代features数组。

foreach ($myJSON_json['features'] as $f) {
    echo $f['properties']['key'].'<br />';
}