如果手中的JSON是您回收的唯一數據,您可以使用json_decode()
創建array()
。
$json = '{"destination_addresses":["Milano, Italia"],"origin_addresses":["Padova PD, Italia"],"rows":[{"elements":[{"distance":{"text":"246 km","value":246492},"duration":{"text":"2 ore 36 min","value":9388},"status":"OK"}]}],"status":"OK"}';
$jsonArray = json_decode($json, true); // get array of json
var_dump($jsonArray);
導致此:
array(4) {
["destination_addresses"] => array(1) {
[0] => string(14) "Milano, Italia"
},
["origin_addresses"] => array(1) {
[0] => string(17) "Padova PD, Italia"
},
["rows"] => array(1) {
[0] => array(1) {
["elements"] => array(1) {
[0] => array(3) {
["distance"] => array(2) {
["text"] => string(6) "246 km",
["value"] => int(246492)
},
["duration"] => array(2) {
["text"] => string(12) "2 ore 36 min",
["value"] => int(9388)
},
["status"] => string(2) "OK"
}
}
之後,你可以做到這一點,以獲得實際的距離:那裏的['0']
的是因爲:
print_r($jsonArray['rows']['0']['elements']['0']['distance']['text']);
// 246 km
注意如果查看print_r($jsonArray);
,您可以看到這些是已轉換的JSON格式文本的一部分。
這裏需要更多細節。這是一個外部文件,變量。把你到目前爲止的PHP代碼。 –
更新代碼.... –
我從捲曲中獲得json。 –