1

When converted to PHP array (not stdClass Object), how can I access geometry to location to lat and long? I've tried $data['results']['geometry']['location']['lat'] and ['lng'] but it didn't work.

I was getting undefined offset and cannot use string offset as array, etc.

{
   "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"
}

3 Answers 3

3

$data['results'] is an indexed array. You just need to fix how you are accessing it:

$data['results'][0]['geometry']['location']['lat']
Sign up to request clarification or add additional context in comments.

2 Comments

Hehe. me too, nem ;-)
No problem, glad to help! @nem sorry! :P
0

Your way:

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

You forgot to put the index

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

Comments

0

You seem to have missed that$data['results'] is an array with one element, so you need to reference that element with [0].

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.