访问php中的json数组

时间:2014-03-28 21:00:25

标签: php json

我有这个json对象,我需要迭代每个文本和值键,请有人帮我这个吗?

我尝试了太多方法,但json旁边没有成功是我的最后一次尝试

提前致谢

[
    {
        "term": [
            {
                "text": "36",
                "value": "36"
            },
            {
                "text": "48",
                "value": "48"
            },
            {
                "text": "60",
                "value": "60"
            },
            {
                "text": "72",
                "value": "72"
            },
            {
                "text": "84",
                "value": "84"
            },
            {
                "text": "96",
                "value": "96"
            },
            {
                "text": "120",
                "value": "120"
            }
        ]
    }
]

我正在尝试这种方式,但没有成功

foreach($Terms['term'] as $key=>$val){ 
    echo $key;
}

1 个答案:

答案 0 :(得分:3)

您需要将JSON解码为数组(或对象),然后才能访问它的元素。

$data = json_decode($jsonString, true);
// according to your data, you have an array inside object, which is inside another array.
foreach ($data[0]['term'] as $key => $value) {
}