如何打印Google Distance Matrix结果Json数据

时间:2016-08-09 11:37:50

标签: php json

如何从Google距离矩阵打印结果iam获得如下结果:

"rows" : [
  {
     "elements" : [
        {
           "distance" : {
              "text" : "4,8 km",
              "value" : 4820
           },
           "duration" : {
              "text" : "17 minutes",
              "value" : 1038
           },
           "status" : "OK"
        },
        {
           "distance" : {
              "text" : "11,1 km",
              "value" : 11064
           },
           "duration" : {
              "text" : "25 minutes",
              "value" : 1506
           },
           "status" : "OK"
        }
     ]
  }
],

我的Php代码打印输出

$json = file_get_contents($url); 
$result = json_decode($json, true); 
for($i=0; $i<count($result['rows']); $i++) {

    echo "Kilometers " . $result['rows'][$i]["elements"][$i]['distance']['text'];

}

。请帮助

2 个答案:

答案 0 :(得分:1)

鉴于您还没有包含所有响应,该方法在数据检索和最终所需输出中都有用,但回答起来很棘手,但我希望以下内容将为如何呈现输出提供指导。

这使用来自interwebs的示例数据而不是问题中给出的部分示例代码 - 可能有点粗糙但是......

$res='{
   "destination_addresses" : [
      "Commons Way, Bridgewater, NJ 08807, USA",
      "Morris Turnpike, Short Hills, NJ 07078, USA",
      "Monmouth Mall, Eatontown, NJ 07724, USA",
      "Garden State Plaza Blvd, Paramus, NJ 07652, USA",
      "Newport Centre Mall, Jersey City, NJ 07302, USA"
   ],
   "origin_addresses" : [ "75 Ninth Ave, New York, NY 10011, USA" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "68.8 km",
                  "value" : 68781
               },
               "duration" : {
                  "text" : "56 mins",
                  "value" : 3334
               },
               "duration_in_traffic" : {
                  "text" : "1 hour 1 min",
                  "value" : 3687
               },
               "status" : "OK"
            },
            {
               "distance" : {
                  "text" : "34.8 km",
                  "value" : 34806
               },
               "duration" : {
                  "text" : "36 mins",
                  "value" : 2138
               },
               "duration_in_traffic" : {
                  "text" : "41 mins",
                  "value" : 2487
               },
               "status" : "OK"
            },
            {
               "distance" : {
                  "text" : "86.3 km",
                  "value" : 86322
               },
               "duration" : {
                  "text" : "1 hour 6 mins",
                  "value" : 3930
               },
               "duration_in_traffic" : {
                  "text" : "1 hour 7 mins",
                  "value" : 4044
               },
               "status" : "OK"
            },
            {
               "distance" : {
                  "text" : "32.3 km",
                  "value" : 32278
               },
               "duration" : {
                  "text" : "33 mins",
                  "value" : 2009
               },
               "duration_in_traffic" : {
                  "text" : "35 mins",
                  "value" : 2082
               },
               "status" : "OK"
            },
            {
               "distance" : {
                  "text" : "6.9 km",
                  "value" : 6879
               },
               "duration" : {
                  "text" : "19 mins",
                  "value" : 1113
               },
               "duration_in_traffic" : {
                  "text" : "24 mins",
                  "value" : 1444
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}';

/* return as an object for easier notation */
$json=json_decode( $res, false );
$origins = $json->origin_addresses;
$destinations=$json->destination_addresses;
/*  if there are multiple rows, use a loop and `$rows[$i]` etc */
$elements=$json->rows[0]->elements;
foreach( $elements as $i => $obj ){

    echo '
    <div>
        <h5>From: '.$origins[ 0 ].' to '.$destinations[ $i ].'</h5>
        Distance: '.$obj->distance->text.',
        Duration:'.$obj->duration->text.',
        In traffic: '.$obj->duration_in_traffic->text.',
        Status: '.$obj->status.'
    </div>';
}

答案 1 :(得分:0)

以下是使用嵌套foreach的示例,这不是渲染输出的唯一方法,但它可以正常工作。

$json = file_get_contents($url);

$result = json_decode($json, true);

foreach ($result['rows'][0] as $elements => $element) {

    foreach ($element as $key => $value) {
        echo $value['distance']['text'];
        echo $value['duration']['text'];
        echo $value['status'];

        echo '<br>';
    }
}

我们所做的就是使用$ key =&gt;访问多维数组; $ value然后$ value [$ key]表示法......我建议一个关于如何在php中处理数组的速成课程,这个很棒:https://www.youtube.com/watch?v=57y5maglayQ

您还可以通过编写echo '<pre>', var_dump($result), '</pre>;'来查看数组的整体结构,这将显示您的数组=&gt;密钥对。