douweidao3882 2010-07-08 00:12
浏览 156
已采纳

PHP:如何从JSON数组中获取属性?

I have the following JSON array:

<?php    
$json = {
  "status": "OK",
  "results": [ {
    "types": [ "street_address" ],
    "formatted_address": "5721 N Northcott Ave, Chicago, IL 60631, USA",
    "address_components": [ {
      "long_name": "5721",
      "short_name": "5721",
      "types": [ "street_number" ]
    }, {
      "long_name": "Illinois",
      "short_name": "IL",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "60631",
      "short_name": "60631",
      "types": [ "postal_code" ]
    } ],
    "geometry": {
      "location": {
        "lat": 41.9858860,
        "lng": -87.7907460
      },
      "location_type": "ROOFTOP",
      "viewport": {
        "southwest": {
          "lat": 41.9827384,
          "lng": -87.7938936
        },
        "northeast": {
          "lat": 41.9890336,
          "lng": -87.7875984
        }
      }
    }
  } ]
};
?>

Using PHP, how do I get the geometery->location->lat&lng values from my JSON array above?

For example (pseudo code):

<?php
$lat = $json['geometry']['location']['lat'];  // 41.9858860
$lng = $json['geometry']['location']['lng'];  // -87.7907460
?>
  • 写回答

3条回答 默认 最新

  • douren7179 2010-07-08 00:17
    关注

    You use json_decode and then $var->results[0]->geometry->location->lat;.

    json_decode yields the following structure:

    object(stdClass)[1]
      public 'status' => string 'OK' (length=2)
      public 'results' => 
        array
          0 => 
            object(stdClass)[2]
              public 'types' => 
                array
                  0 => string 'street_address' (length=14)
              public 'formatted_address' => string '5721 N Northcott Ave, Chicago, IL 60631, USA' (length=44)
              public 'address_components' => 
                array
                  0 => 
                    object(stdClass)[3]
                      public 'long_name' => string '5721' (length=4)
                      public 'short_name' => string '5721' (length=4)
                      public 'types' => 
                        array
                          0 => string 'street_number' (length=13)
                  1 => 
                    object(stdClass)[4]
                      public 'long_name' => string 'Illinois' (length=8)
                      public 'short_name' => string 'IL' (length=2)
                      public 'types' => 
                        array
                          0 => string 'administrative_area_level_1' (length=27)
                          1 => string 'political' (length=9)
                  2 => 
                    object(stdClass)[5]
                      public 'long_name' => string '60631' (length=5)
                      public 'short_name' => string '60631' (length=5)
                      public 'types' => 
                        array
                          0 => string 'postal_code' (length=11)
              public 'geometry' => 
                object(stdClass)[6]
                  public 'location' => 
                    object(stdClass)[7]
                      public 'lat' => float 41.985886
                      public 'lng' => float -87.790746
                  public 'location_type' => string 'ROOFTOP' (length=7)
                  public 'viewport' => 
                    object(stdClass)[8]
                      public 'southwest' => 
                        object(stdClass)[9]
                          public 'lat' => float 41.9827384
                          public 'lng' => float -87.7938936
                      public 'northeast' => 
                        object(stdClass)[10]
                          public 'lat' => float 41.9890336
                          public 'lng' => float -87.7875984
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误