Get JSON value from array using Laravel

I am trying to get latitude and longitude values ​​from a JSON array - $response

returned from google from my geocoding services.

The JSON array is returned as such (random address):

{
   "results":[
      {
         "address_components":[
            {
               "long_name":"57",
               "short_name":"57",
               "types":[
                  "street_number"
               ]
            },
            {
               "long_name":"Polo Gardens",
               "short_name":"Polo Gardens",
               "types":[
                  "route"
               ]
            },
            {
               "long_name":"Bucksburn",
               "short_name":"Bucksburn",
               "types":[
                  "sublocality_level_1",
                  "sublocality",
                  "political"
               ]
            },
            {
               "long_name":"Aberdeen",
               "short_name":"Aberdeen",
               "types":[
                  "locality",
                  "political"
               ]
            },
            {
               "long_name":"Aberdeen",
               "short_name":"Aberdeen",
               "types":[
                  "postal_town"
               ]
            },
            {
               "long_name":"Aberdeen City",
               "short_name":"Aberdeen City",
               "types":[
                  "administrative_area_level_2",
                  "political"
               ]
            },
            {
               "long_name":"United Kingdom",
               "short_name":"GB",
               "types":[
                  "country",
                  "political"
               ]
            },
            {
               "long_name":"AB21 9JU",
               "short_name":"AB21 9JU",
               "types":[
                  "postal_code"
               ]
            }
         ],
         "formatted_address":"57 Polo Gardens, Aberdeen, Aberdeen City AB21 9JU, UK",
         "geometry":{
            "location":{
               "lat":57.1912463,
               "lng":-2.1790257
            },
            "location_type":"ROOFTOP",
            "viewport":{
               "northeast":{
                  "lat":57.19259528029149,
                  "lng":-2.177676719708498
               },
               "southwest":{
                  "lat":57.18989731970849,
                  "lng":-2.180374680291502
               }
            }
         },
         "partial_match":true,
         "place_id":"ChIJLTex1jQShEgR5UJ2DNc6N9s",
         "types":[
            "street_address"
         ]
      }
   ],
   "status":"OK"
}

      

I've tried the following:

json_decode($response->results->geometry->location->lat)

      

But it returns "trying to access a non-object property".

Any help would be greatly appreciated.

+3


source to share


3 answers


var_dump(json_decode($response)->results[0]->geometry->location->lat);

      



+3


source


Alternatively, pass true as the second parameter to the json_decode function and use the array notation:



$obj = json_decode($json_string2, true);
foreach ($obj['geometry'] as $key => $value) {
    $lat = $value['location']['lat'];
    $long   = $value['location']['lng'];
}

      

+2


source


Try the following: $jsonArray = json_decode($response,true);

It's simple, first you need to convert the whole json string to an array or object using a function json_decode

and then work with the structure.

0


source







All Articles