2015-08-08 43 views
0

我開始了我的php知識,我在閱讀json的這個數組時遇到困難,我可以讀取所有的信息,但是我不能讀取geofences數組中的數據。和名字? 我的JSON例子當我在php中閱讀這個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']; 

    } 
+0

您是否收到任何錯誤? – Madness

+0

注意:未定義的索引:geoFences,如果我使用echo $ person ['geoFences'] ['id']; – user13423

回答

0

你有geoFences下的另一個數組。 這是循環:

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