I'm using the following code to get the distance between location.
<?php
function curl_request($sURL,$sQueryString=null)
{
$cURL=curl_init();
curl_setopt($cURL,CURLOPT_URL,$sURL.'?'.$sQueryString);
curl_setopt($cURL,CURLOPT_RETURNTRANSFER, TRUE);
$cResponse=trim(curl_exec($cURL));
curl_close($cURL);
return $cResponse;
}
$sResponse=curl_request('http://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=London&destinations=Southend-on-sea|Westcliff-on-sea|Leigh-on-sea|leeds&mode=driving&language=en&sensor=false');
$oJSON=json_decode($sResponse);
if ($oJSON->status=='OK')
$fDistanceInMiles=(float)preg_replace('/[^\d\.]/','',$oJSON->rows[0]->elements[0]->distance->text);
else
$fDistanceInMiles=0;
echo 'Distance in Miles: '.$fDistanceInMiles.PHP_EOL;
?>
-
This will only get the first value in the JSON response because of this:
$fDistanceInMiles=(float)preg_replace('/[^\d\.]/','',$oJSON->rows[0]->elements[0]->distance->text);
-
The JSON looks like this:
{
"destination_addresses" : [
"Southend-on-Sea, UK",
"Westcliff-on-Sea, Southend-on-Sea, UK",
"Leigh-on-Sea SS9, UK",
"Leeds, UK"
],
"origin_addresses" : [ "London, UK" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "42.0 mi",
"value" : 67669
},
"duration" : {
"text" : "1 hour 14 mins",
"value" : 4464
},
"status" : "OK"
},
{
"distance" : {
"text" : "42.7 mi",
"value" : 68723
},
"duration" : {
"text" : "1 hour 17 mins",
"value" : 4646
},
"status" : "OK"
},
{
"distance" : {
"text" : "40.1 mi",
"value" : 64508
},
"duration" : {
"text" : "1 hour 10 mins",
"value" : 4225
},
"status" : "OK"
},
{
"distance" : {
"text" : "195 mi",
"value" : 313043
},
"duration" : {
"text" : "3 hours 39 mins",
"value" : 13133
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
So when i run my code, I get this printed on my page:
Distance in Miles: 42
But what I need is to print out the highest number. So it should be like this:
Distance in Miles: 195
Could someone please advise on this?
Thanks in advance.