1

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!

3
  • 1
    Paste your JSON into array.include-once.org for examples. Commented Jul 28, 2013 at 22:54
  • make it into an array by simply adding true as the second arg. for example: json_decode($MY_JSON_STRING, true); Commented Jul 28, 2013 at 23:03
  • That is a great website! Helped me understand JSONObjects and how they work. For anyone in the future to get the end co-ordinates use $array->routes[0]->legs[0]->end_location->lat; Commented Jul 28, 2013 at 23:22

1 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

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.