Reputation: 65
I'm confused how to parse a JSON object in PHP.
I have this object from Google API (I've removed my address details!):
stdClass Object (
[routes] => Array (
[0] => stdClass Object (
[bounds] => stdClass Object (
[northeast] => stdClass Object (
[lat] => 0.000000
[lng] => 0.000000
)
[southwest] => stdClass Object (
[lat] => 0.000000
[lng] => 0.00000
)
)
[copyrights] => Map data ©2013 Google
[legs] => Array (
[0] => stdClass Object (
[distance] => stdClass Object (
[text] => 1 ft
[value] => 0
)
[duration] => stdClass Object (
[text] => 1 min
[value] => 0
)
[end_address] => xxxx
[end_location] => stdClass Object (
[lat] => 0.0000
[lng] => 0.0000
)
[start_address] => xx
[start_location] => stdClass Object (
[lat] => 0.0000
[lng] => 0.0000
)
[steps] => Array (
[0] => stdClass Object (
[distance] => stdClass Object (
[text] => 1 ft
[value] => 0
)
[duration] => stdClass Object (
[text] => 1 min
[value] => 0
)
[end_location] => stdClass Object (
[lat] => 0.0000
[lng] => 0.0000
)
[html_instructions] => xxx
[polyline] => stdClass Object (
[points] => wcrnIpdvH
)
[start_location] => stdClass Object (
[lat] => 1.234567
[lng] => 1.234567
)
[travel_mode] => DRIVING
)
)
[via_waypoint] => Array (
)
)
)
[overview_polyline] => stdClass Object (
[points] => wcrnIpdvH
)
[summary] => xx
[warnings] => Array (
)
[waypoint_order] => Array (
)
)
)
[status] => OK
)
I want to get the last latitude and longitude which I've left as [lat] => 1.234567 [lng] => 1.234567
I've worked out how to get the distance by doing this:
$directions = json_decode($return);
$distance = $directions->routes[0]->legs[0]->distance->text;
However, getting the longitude and latitude has stumped me.
If you could explain JSONs a little thanks more so!
Upvotes: 1
Views: 1080
Reputation: 15356
Try the following:
//northeast
$lat = $directions->routes[0]->bounds->northeast->lat;
$lng = $directions->routes[0]->bounds->northeast->lng;
and
////southwest
$lat = $directions->routes[0]->bounds->southwest->lat;
$lng = $directions->routes[0]->bounds->southwest->lng;
Upvotes: 1