0

I am trying to get the driving distance from google maps.

print_r($result); outputs the below:

Array
    (
        [0] => Array
            (
                [elements] => Array
                    (
                        [0] => Array
                            (
                                [distance] => Array
                                    (
                                        [text] => 3,936 km
                                        [value] => 3935862
                                    )

                                [duration] => Array
                                    (
                                        [text] => 1 day 18 hours
                                        [value] => 150860
                                    )

                                [status] => OK
                            )

                    )

            )

    )

my php code to produce this is:

$q = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=Sydney&destinations=Perth&mode=driving&sensor=false";
$json = file_get_contents($q);
$details = json_decode($json, TRUE);
$details=$details['rows'];
print_r($details);

I somehow need to access this array and get the distance text value, in this case 3936 km

Any help appreciated, Thanks as always

4
  • $result['elements'][0]['distance']['text'] Commented Aug 16, 2014 at 12:57
  • Thanks Aleksandar... didnt quite work. see fiddle link on question at bottom. thanks again. output is empty. Commented Aug 16, 2014 at 13:16
  • your link is not good, it gives INVALID_REQUEST Commented Aug 16, 2014 at 13:24
  • Thanks, I have updated the question with my php that produces the error. thanks again. Commented Aug 16, 2014 at 13:29

2 Answers 2

1
<?php
$q = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=Sydney&destinations=Perth&mode=driving&sensor=false";
$json = file_get_contents($q);
$details = json_decode($json, TRUE);
$details=$details['rows'][0]['elements'][0]['distance']['text'];
print_r($details);
?>

there you go

Sign up to request clarification or add additional context in comments.

Comments

0

If we assume that variable $array holds the array you have attached to your post then we can do the following:

echo $array['elements'][0]['distance']['text']; //should output 1300km

Hope it helps!

4 Comments

Thanks, didnt quite work. see fiddle link on question at bottom. thanks again
Of course it did not worked, your $q has a syntax error. (at least the fiddle you posted).
Please use pastebin or any other simmiliar service to share your code so we can proceed in helping you!
Array you have attached to the question, is different from the one you get from the API :) just a bit see the pastebin above ^^

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.