Reputation: 526
I am very sorry if this is really obvious how ever I am really struggling to get this to work.
I call the url as follows:
$url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=$postcode1&destinations=$postcode2&mode=driving&language=en-EN&sensor=false&key=MY-KEY";
I have the following return array:
Array
(
[destination_addresses] => Array
(
[0] => Addres_1
)
[origin_addresses] => Array
(
[0] => Address_2
)
[rows] => Array
(
[0] => Array
(
[elements] => Array
(
[0] => Array
(
[distance] => Array
(
[text] => 18.9 km
[value] => 18862
)
[duration] => Array
(
[text] => 20 mins
[value] => 1184
)
[status] => OK
)
)
)
)
[status] => OK
)
I am trying to get the distance in a string as follows and the echo it out
$data = file_get_contents($url);
$result = json_decode($data, true);
$distance = $result['rows'][0]['elements'][0]['distance']['text']['value'];
and the echo $distance
this echo's out 1
where it should be 18862
any help welcome
Upvotes: 2
Views: 46
Reputation: 6679
Just remove the ['text']
from the:
$distance = $result['rows'][0]['elements'][0]['distance']['text']['value'];
Making it:
$distance = $result['rows'][0]['elements'][0]['distance']['value'];
text
isn't a parent of value
Upvotes: 2