dpikoto468637 2013-06-20 01:17
浏览 33
已采纳

访问JSON数组PHP中的值

I am not having any success getting values out of a JSON array object. I am specifically trying to get to geometry->location->lat

Here is what I am doing:

    $url='http://maps.googleapis.com/maps/api/geocode/json?address='.$a.'&sensor=false';
    $result = file_get_contents($url);
    $jsonArr = json_decode($result,true);

    echo "Lat: ".$jsonArr['results']['geometry']['location']['lat'];

Here is a print_r() of the JSON returned by Google:

 {
       "results" : [
          {
             "address_components" : [
                {
                   "long_name" : "46814",
                   "short_name" : "46814",
                   "types" : [ "postal_code" ]
                },
                {
                   "long_name" : "Fort Wayne",
                   "short_name" : "Fort Wayne",
                   "types" : [ "locality", "political" ]
                },
                {
                   "long_name" : "Indiana",
                   "short_name" : "IN",
                   "types" : [ "administrative_area_level_1", "political" ]
                },
                {
                   "long_name" : "United States",
                   "short_name" : "US",
                   "types" : [ "country", "political" ]
                }
             ],
             "formatted_address" : "Fort Wayne, IN 46814, USA",
             "geometry" : {
                "bounds" : {
                   "northeast" : {
                      "lat" : 41.08472709999999,
                      "lng" : -85.26534090
                   },
                   "southwest" : {
                      "lat" : 41.00163510,
                      "lng" : -85.35540899999999
                   }
                },
                "location" : {
                   "lat" : 41.05472940,
                   "lng" : -85.30328109999999
                },
                "location_type" : "APPROXIMATE",
                "viewport" : {
                   "northeast" : {
                      "lat" : 41.08472709999999,
                      "lng" : -85.26534090
                   },
                   "southwest" : {
                      "lat" : 41.00163510,
                      "lng" : -85.35540899999999
                   }
                }
             },
             "types" : [ "postal_code" ]
          }
       ],
       "status" : "OK"
    }
  • 写回答

1条回答 默认 最新

  • dqz86173 2013-06-20 01:22
    关注

    The results are returned in an array. Get the first element and you're done. However you should make sure that you actually have at least one element in the results array.

    echo "Lat: " . $jsonArr['results'][0]['geometry']['location']['lat'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?