I am pulling some JSON data from google maps that gives me an accuret distance between two addresses. That works fine but when it comes to using json_decode it gives me some horrific data! This is the example array thats output:
array(4) {
["destination_addresses"]=>
array(1) {
[0]=>
string(51) "96 Stirling Street, Alva, Clackmannanshire FK12, UK"
}
["origin_addresses"]=>
array(1) {
[0]=>
string(64) "17 Dalgety Road, Edinburgh, Edinburgh, City of Edinburgh EH7, UK"
}
["rows"]=>
array(1) {
[0]=>
array(1) {
["elements"]=>
array(1) {
[0]=>
array(3) {
["distance"]=>
array(2) {
["text"]=>
string(7) "39.3 mi"
["value"]=>
int(63242)
}
["duration"]=>
array(2) {
["text"]=>
string(7) "59 mins"
["value"]=>
int(3555)
}
["status"]=>
string(2) "OK"
}
}
}
}
["status"]=>
string(2) "OK"
}
AS you can see it also puts string(#) or int(#) at the start of the array item and " around the value. Whats causing this issues? Heres the outputted JSON.
{
"destination_addresses" : [ "96 Stirling Street, Alva, Clackmannanshire FK12, UK" ],
"origin_addresses" : [ "17 Dalgety Road, Edinburgh, Edinburgh, City of Edinburgh EH7, UK" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "39.3 mi",
"value" : 63242
},
"duration" : {
"text" : "59 mins",
"value" : 3555
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
My php code:
$url = fopen("https://maps.googleapis.com/maps/api/distancematrix/json?origins=".$latitudeFrom.",".$longitudeFrom."&destinations=".$latitudeTo.",".$longitudeTo."&mode=driving&units=imperial", "r");
$result = json_decode(stream_get_contents($url), true);
fclose($url);
var_dump($result['rows'][0]['elements'][0]['distance']['text']);
var_dump, which is great for debugging, not so good for data exchange.