2

I got from the Google API a JSON code that shows the distance and time between two places by car.

With json_decode I created a array of it in PHP.

This is the array that I get when using print_r:

Array
(
    [destination_addresses] => Array
        (
            [0] => Rachelsmolen 1, 5612 MA Eindhoven, Netherlands
        )

    [origin_addresses] => Array
        (
            [0] => Rachelsmolen 1, 5612 MA Eindhoven, Netherlands
        )

    [rows] => Array
        (
            [0] => Array
                (
                    [elements] => Array
                        (
                            [0] => Array
                                (
                                    [distance] => Array
                                        (
                                            [text] => 1 m
                                            [value] => 0
                                        )

                                    [duration] => Array
                                        (
                                            [text] => 1 min
                                            [value] => 0
                                        )

                                    [status] => OK
                                )

                        )

                )

        )

    [status] => OK
)

How can I echo the "1 m" (under [distance] => Array) in my PHP code?

---EDIT--- This is the code that gives me te array above:

$link = "https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=" . $latitudeStart . "," . $longitudeStart . "&destinations=" . $latitudeStop . "," . $longitudeStop . "&key=MyGoogleApiKey";

$json = file_get_contents($link);
$data = json_decode($json, TRUE);
echo "<pre>"; print_r($data); echo "</pre>";
Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
Stefan
  • 35
  • 1
  • 6

1 Answers1

2

If you want it statically, you can use following line to echo

echo $response['rows'][0]['elements'][0]['distance']['text'];

Note: Please change $response into your variable.

Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50