簡體   English   中英

訪問PHP數組中的鍵值對

[英]Accessing key-value pair in PHP array

當轉換為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']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM