如何将Google Maps API v3距离矩阵的JSON输出解析为PHP?

0 php json google-maps-api-3

我只是想将Google Maps API距离矩阵的输出转换为PHP以进行进一步处理.我无法弄清楚如何将距离值恢复为变量.我的数据是来自Google的Maps API v3的JSON文件.

{
   "destination_addresses" : [ "San Francisco, Californie, États-Unis", "Victoria, BC, Canada" ],
   "origin_addresses" : [ "Vancouver, BC, Canada", "Seattle, Washington, États-Unis" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "1 703 km",
                  "value" : 1703343
               },
               "duration" : {
                  "text" : "3 jours 19 heures",
                  "value" : 326836
               },
               "status" : "OK"
            },
            {
               "distance" : {
                  "text" : "138 km",
                  "value" : 138267
               },
               "duration" : {
                  "text" : "6 heures 45 minutes",
                  "value" : 24278
               },
               "status" : "OK"
            }
         ]
      },
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "1 451 km",
                  "value" : 1451182
               },
               "duration" : {
                  "text" : "3 jours 4 heures",
                  "value" : 274967
               },
               "status" : "OK"
            },
            {
               "distance" : {
                  "text" : "146 km",
                  "value" : 146496
               },
               "duration" : {
                  "text" : "2 heures 52 minutes",
                  "value" : 10321
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}
Run Code Online (Sandbox Code Playgroud)

我是javascript和JSON的新手,拥有最少的PHP经验.

ham*_*czu 5

$url = 'http://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver+BC|Seattle&destinations=San+Francisco|Victoria+BC&mode=bicycling&language=fr-FR&sensor=false';
$json = file_get_contents($url); // get the data from Google Maps API
$result = json_decode($json, true); // convert it from JSON to php array
echo $result['rows'][0]['elements'][0]['distance']['text'];
Run Code Online (Sandbox Code Playgroud)