将Google API JSON响应转换为单个php变量

将Google API JSON响应转换为单个php变量,php,json,api,Php,Json,Api,我有一个网页,它包含两个邮政编码,并将距离与谷歌发布的其他信息分开 守则: $url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=$postcode1&destinations=$postcode2&mode=bicycling&language=en-EN&sensor=false"; $data = @file_get_contents($url); $result =

我有一个网页,它包含两个邮政编码,并将距离与谷歌发布的其他信息分开

守则:

$url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=$postcode1&destinations=$postcode2&mode=bicycling&language=en-EN&sensor=false";
$data = @file_get_contents($url);

$result = json_decode($data, true);

print_r($result);
结果是:

{
   "destination_addresses" : [ "Pound Rd, Oldbury B68 8NE, UK" ],
   "origin_addresses" : [ "Vyse St, Birmingham B18 6NE, UK" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "10.4 km",
                  "value" : 10384
               },
               "duration" : {
                  "text" : "36 mins",
                  "value" : 2133
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}
我对JSON还不熟悉,所以如果这听起来很愚蠢,请原谅。我试了不少。最接近我的是

echo $result['destination_addresses'][0];
返回目标地址。我想知道如何单独获取所有内容,尤其是距离文本

提前谢谢


Jake32

以下是解决方案:-

echo "Destination Address=".$result['destination_addresses'][0]."<br>";
echo "Origin Address=".$result['origin_addresses'][0]."<br>";
echo "Distance text=".$result['rows'][0]['elements'][0]['distance']['text']."<br>";
echo“Destination Address=”.$result['Destination_addresses'][0]。“
”; echo“Origin Address=“.result['Origin_addresses'][0]。”
; echo“Distance text=”.$result['rows'][0]['elements'][0]['Distance']['text'].“
”;