user756659
user756659

Reputation: 3512

php parsing json data

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.

Upvotes: 3

Views: 50

Answers (4)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

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'];
}

Upvotes: 3

Dinuka Thilanga
Dinuka Thilanga

Reputation: 4330

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;

Upvotes: 1

Duke
Duke

Reputation: 36970

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;
}

Upvotes: 1

Subodh Ghulaxe
Subodh Ghulaxe

Reputation: 18651

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;

Upvotes: 2

Related Questions