我從Google地圖拉出一些JSON數據,這給了我兩個地址之間的確定距離。這工作正常,但是當涉及到使用json_decode它給我一些可怕的數據!這是例如數組多數民衆贊成輸出:?JSON_decode問題返回帶有數據的字符串()和int()
array(4) {
["destination_addresses"]=>
array(1) {
[0]=>
string(51) "96 Stirling Street, Alva, Clackmannanshire FK12, UK"
}
["origin_addresses"]=>
array(1) {
[0]=>
string(64) "17 Dalgety Road, Edinburgh, Edinburgh, City of Edinburgh EH7, UK"
}
["rows"]=>
array(1) {
[0]=>
array(1) {
["elements"]=>
array(1) {
[0]=>
array(3) {
["distance"]=>
array(2) {
["text"]=>
string(7) "39.3 mi"
["value"]=>
int(63242)
}
["duration"]=>
array(2) {
["text"]=>
string(7) "59 mins"
["value"]=>
int(3555)
}
["status"]=>
string(2) "OK"
}
}
}
}
["status"]=>
string(2) "OK"
}
,你可以看到它也讓字符串(#)或INT(#)在陣列項目的啓動和「圍繞價值請告訴我造成這一問題的繼承人輸出的JSON
{
"destination_addresses" : [ "96 Stirling Street, Alva, Clackmannanshire FK12, UK" ],
"origin_addresses" : [ "17 Dalgety Road, Edinburgh, Edinburgh, City of Edinburgh EH7, UK" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "39.3 mi",
"value" : 63242
},
"duration" : {
"text" : "59 mins",
"value" : 3555
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
我的PHP代碼:。
$url = fopen("https://maps.googleapis.com/maps/api/distancematrix/json?origins=".$latitudeFrom.",".$longitudeFrom."&destinations=".$latitudeTo.",".$longitudeTo."&mode=driving&units=imperial", "r");
$result = json_decode(stream_get_contents($url), true);
fclose($url);
var_dump($result['rows'][0]['elements'][0]['distance']['text']);
這看起來像var_dump'的'的輸出,這對於調試是偉大的,數據也不是那麼好交換。 – Halcyon 2014-11-24 17:12:54
是的,但我要去:echo $ array ['rows'] [0] ['elements'] [0] ['distance'] ['test']; 我會得到'串(7)'39.3英里'',而不是'39.3英里'正如所料! – Jiggles 2014-11-24 17:14:25