【问题标题】:Getting Values from JSON Array using PHP使用 PHP 从 JSON 数组中获取值
【发布时间】:2015-01-27 11:46:33
【问题描述】:

这是我的要求

$GoogleApiResult = file_get_contents("https://maps.googleapis.com/maps/api/distancematrix/json?origins=11.0450,76.9200&destinations=11.0450,76.9455");
return $GoogleApiResult;

我的回应是

{ "destination_addresses" : [ "201, Koundampalayam, Coimbatore, Tamil Nadu 641030, India" ], "origin_addresses" : [ "1, Luna Plastics Road, Lakshmi Nagar, Coimbatore, Tamil Nadu 641025, India" ], "rows" : [ { "elements" : [ { "distance" : { "text" : "3.5 km", "value" : 3523 }, "duration" : { "text" : "6 mins", "value" : 382 }, "status" : "OK" } ] } ], "status" : "OK" }

如何获得距离(3.5 公里)和持续时间(6 分钟)的值?

【问题讨论】:

    标签: php arrays json google-api


    【解决方案1】:

    您需要使用 json_decode 函数将 json 响应转换为 PHP 数组。

    使用以下代码从 api 获取响应的持续时间和距离:

    $array=json_decode($json); //$json is api response
    //var_dump($array);
    
    echo $array->rows[0]->elements[0]->distance->text;
    echo $array->rows[0]->elements[0]->duration->text;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-15
      • 2020-01-25
      • 1970-01-01
      相关资源
      最近更新 更多