当我在php中读到这个Json?

时间:2015-08-08 03:39:37

标签: php json

我开始了解php的知识,我在json中读取这个数组很困难,我可以读取所有信息,但是我无法读取geofences数据中的数据

如何阅读“geoFences”id和名称中包含的信息? 我的json示例

我需要在php json中阅读此内容。

$response =
    [
    {
    "id":441818,
    "device":{
    "id":6,
    "uniqueId":"35390906000",
    "name":"440",
    "description":"COOR",
    "timeout":3000,
    "idleSpeedThreshold":0.0,
    "iconType":{
    "OFFLINE":{
    "width":21,
    "height":25,
    "urls":[
    "/img/marker-white.png",
    "http://cdnjs.cloudflare.com/ajax/libs/openlayers/2.13.1/img/marker-green.png"
    ]
    },
    "LATEST":{
    "width":21,
    "height":25,
    "urls":[
    "http://cdnjs.cloudflare.com/ajax/libs/openlayers/2.13.1/img/marker.png",
    "http://cdnjs.cloudflare.com/ajax/libs/openlayers/2.13.1/img/marker-green.png"
    ]
    }
    }
    },
    "time":"Fri, 07 Aug 2015 23:13:06 -0300",
    "valid":true,
    "latitude":-23.1,
    "longitude":-46.1,
    "altitude":745.0,
    "speed":0.0,
    "course":0.0,
    "other":"\u003cinfo\u003e\u003cbattery\u003e58\u003c/battery\u003e\u003cip\u003e179.95.37.54\u003c/ip\u003e\u003c/info\u003e",
    "geoFences":[
    {
    "id":16,
    "name":"PL Eldorado",
    "color":"20B2AA"
    }
    ]
    }
    ]

我需要在php中阅读这些信息...

"id":16,
"name":"PL Eldorado".

例如,如果我需要读取设备 - >名称我这样做:

$json=json_decode($response, true);
foreach ($json as $person_name => $person) 
{   
    echo $person['device']['name'];

}

但我这样做,不行。

   $json=json_decode($response, true);
    foreach ($json as $person_name => $person) 
    {   
        echo $person['geoFences']['id'];
        echo $person['geoFences']['name'];

    }

1 个答案:

答案 0 :(得分:0)

您在geoFences下有另一个数组。 这是循环:

foreach($json as $person){
    foreach($person['geoFences'] as $g){
        echo $g['id'].'<br>';
        echo $g['name'].'<br>';
    }   
}