If that piece of JSON is the only data you get back you can use json_decode()
to create an array()
.
$json = '{"destination_addresses":["Milano, Italia"],"origin_addresses":["Padova PD, Italia"],"rows":[{"elements":[{"distance":{"text":"246 km","value":246492},"duration":{"text":"2 ore 36 min","value":9388},"status":"OK"}]}],"status":"OK"}';
$jsonArray = json_decode($json, true); // get array of json
var_dump($jsonArray);
Which results in this:
array(4) {
["destination_addresses"] => array(1) {
[0] => string(14) "Milano, Italia"
},
["origin_addresses"] => array(1) {
[0] => string(17) "Padova PD, Italia"
},
["rows"] => array(1) {
[0] => array(1) {
["elements"] => array(1) {
[0] => array(3) {
["distance"] => array(2) {
["text"] => string(6) "246 km",
["value"] => int(246492)
},
["duration"] => array(2) {
["text"] => string(12) "2 ore 36 min",
["value"] => int(9388)
},
["status"] => string(2) "OK"
}
}
And after that you can do this to get the actual distance:
print_r($jsonArray['rows']['0']['elements']['0']['distance']['text']);
// 246 km
Note: the ['0']
's are there because if look at the print_r($jsonArray);
you can see these are part of the converted JSON formatted text.