2016-12-08 46 views
2

我从Google API获取了一个JSON代码,该代码显示两个车之间的距离和时间。从php数组获取价值(使用JSON创建)

With json_decode我在PHP中创建了一个数组。

这是我使用print_r时获取数组:

Array 
(
    [destination_addresses] => Array 
     (
      [0] => Rachelsmolen 1, 5612 MA Eindhoven, Netherlands 
     ) 

    [origin_addresses] => Array 
     (
      [0] => Rachelsmolen 1, 5612 MA Eindhoven, Netherlands 
     ) 

    [rows] => Array 
     (
      [0] => Array 
       (
        [elements] => Array 
         (
          [0] => Array 
           (
            [distance] => Array 
             (
              [text] => 1 m 
              [value] => 0 
             ) 

            [duration] => Array 
             (
              [text] => 1 min 
              [value] => 0 
             ) 

            [status] => OK 
           ) 

         ) 

       ) 

     ) 

    [status] => OK 
) 

我怎么能呼应 “1 M”([distance] => Array下)在我的PHP代码?

---编辑--- 这是给我上面TE数组的代码:如果你想要静态

$link = "https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=" . $latitudeStart . "," . $longitudeStart . "&destinations=" . $latitudeStop . "," . $longitudeStop . "&key=MyGoogleApiKey"; 

$json = file_get_contents($link); 
$data = json_decode($json, TRUE); 
echo "<pre>"; print_r($data); echo "</pre>"; 
+0

可以显示你的努力,直到现在(你的PHP代码)? –

+0

请用您的密码编辑您的问题。 –

+0

现在加入它,但哈桑的作品的答案。 – Stefan

回答

2

,您可以使用以下行来echo

echo $response['rows'][0]['elements'][0]['distance']['text']; 

注意:请将$response更改为您的变量。

+0

不知道这是如此简单,感谢您的快速帮助。 – Stefan

+0

@Stefan欢迎您。如果您发现我的答案有用,请打勾。 – Hassaan