在PHP数组中访问键值对

在PHP数组中访问键值对,php,arrays,Php,Arrays,当转换为PHP数组(不是stdClass对象)时,如何访问几何体以定位到lat和long?我试过$data['results']['geometry']['location']['lat']和['lng']但都不起作用 我得到了未定义的偏移量,无法将字符串偏移量用作数组等 { "results" : [ { "address_components" : [ { "long_name" : "1600",

当转换为PHP数组(不是stdClass对象)时,如何访问几何体以定位到lat和long?我试过$data['results']['geometry']['location']['lat']和['lng']但都不起作用

我得到了未定义的偏移量,无法将字符串偏移量用作数组等

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "1600",
               "short_name" : "1600",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Amphitheatre Parkway",
               "short_name" : "Amphitheatre Pkwy",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Mountain View",
               "short_name" : "Mountain View",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Santa Clara County",
               "short_name" : "Santa Clara County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "94043",
               "short_name" : "94043",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
         "geometry" : {
            "location" : {
               "lat" : 37.4222953,
               "lng" : -122.0840671
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.42364428029151,
                  "lng" : -122.0827181197085
               },
               "southwest" : {
                  "lat" : 37.42094631970851,
                  "lng" : -122.0854160802915
               }
            }
         },
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}

$data['results']
是一个索引数组。您只需修复访问它的方式:

$data['results'][0]['geometry']['location']['lat']
你的方式:

$data['results']['geometry']['location']['lat'] // same for ['lng']
你忘了放索引了

$data['results'][0]['geometry']['location']['lat'] // same for ['lng']

您似乎忽略了
$data['results']
是一个包含一个元素的数组,因此需要使用
[0]
引用该元素


试试:
$data['results'][0]['geometry']['location']['lat']

Hehe。我也是,尼姆;-)没问题,很乐意帮忙@对不起P