3

Never had to do this one and getting confused about the format and how to access specific variables out of it.

The result of :

$result = json_decode($result);
print_r($result);

is :

stdClass Object
(
 [input] => stdClass Object
 (
 [address_components] => stdClass Object
 (
 [number] => 1109
 [predirectional] => N
 [street] => Highland
 [suffix] => St
 [formatted_street] => N Highland St
 [city] => Arlington
 [state] => VA
 [zip] => 22201
 [country] => US
 )
 [formatted_address] => 1109 N Highland St, Arlington, VA 22201
 )
 [results] => Array
 (
 [0] => stdClass Object
 (
 [address_components] => stdClass Object
 (
 [number] => 1109
 [predirectional] => N
 [street] => Highland
 [suffix] => St
 [formatted_street] => N Highland St
 [city] => Arlington
 [county] => Arlington County
 [state] => VA
 [zip] => 22201
 [country] => US
 )
 [formatted_address] => 1109 N Highland St, Arlington, VA 22201
 [location] => stdClass Object
 (
 [lat] => 38.886672
 [lng] => -77.094735
 )
 [accuracy] => 1
 [accuracy_type] => rooftop
 [source] => Arlington
 )
 [1] => stdClass Object
 (
 [address_components] => stdClass Object
 (
 [number] => 1109
 [predirectional] => N
 [street] => Highland
 [suffix] => St
 [formatted_street] => N Highland St
 [city] => Arlington
 [county] => Arlington County
 [state] => VA
 [zip] => 22201
 [country] => US
 )
 [formatted_address] => 1109 N Highland St, Arlington, VA 22201
 [location] => stdClass Object
 (
 [lat] => 38.886665
 [lng] => -77.094733
 )
 [accuracy] => 1
 [accuracy_type] => rooftop
 [source] => Virginia Geographic Information Network (VGIN)
 )
 )
)

How can I access the lat and lng values? I am getting confused since there are objects and arrays mixed within. I've never worked with json in this manner with php... it is a geocoding api that returns in json format.

I basically want to do something like $lat = $result['results'][0]['location']['lat']; and so on.

asked Oct 10, 2017 at 4:52
1
  • 1
    Use type casting, convert it into object Commented Oct 10, 2017 at 4:54

4 Answers 4

3

You have to use true as second parameter in json_decode so that output will be normal php array not stdclass array(so that you can use your approach what you are trying)

So do like below:-

$result = json_decode($result,true);
echo $lat = $result['results'][0]['location']['lat'];
echo $lng = $result['results'][0]['location']['lng'];

Note:-

1. With your current format also you can get data like below:-

echo $lat = $result->results[0]->location->lat; //using object approach 
echo $lng = $result->results[0]->location->lng; //using object approach 

2. Use foreach() to get all lat-lng records.

foreach($result->results as $val) { //if using stdclass array
 echo "Latitude = ".$val->location->lat;
 echo "Longitude = ".$val->location->lng;
}

Or:-

foreach($result['results'] as $val) {//if using normal array
 echo "Latitude = ".$val['location']['lat'];
 echo "Longitude = ".$val['location']['lng'];
}
answered Oct 10, 2017 at 4:55
2
  • 1
    Knew it had to be something simple I was missing... that is what I was looking for... thanks! Commented Oct 10, 2017 at 4:59
  • @user756659 glad to help you :):) Commented Oct 10, 2017 at 5:32
2

You can access lat lng from result as below

$result = json_decode($result);
echo $result->results[0]->location->lat;
echo $result->results[0]->location->lng;
answered Oct 10, 2017 at 5:01
1

I can see that from your example you have multiple records to fetch. Use a foreach() to print the details

$result = json_decode($result);
foreach($result->results as $value) {
 echo "Lat--",$value->location->lat; //using object
 echo "Long--",$value->location->lng;
}
answered Oct 10, 2017 at 5:04
1

Use second parameter as true for convert all object to array. Otherwise you can get values like this.

$result->results[0]->location->lat;
$result->results[0]->location->lng;
Death-is-the-real-truth
72.3k10 gold badges58 silver badges105 bronze badges
answered Oct 10, 2017 at 4:58
1
  • 1
    it's lng not long. Now +1 because you are correct too. Commented Oct 10, 2017 at 5:12

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.