-1

For example :

 {
   "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"
}

i want to get distance in this case 246km Thanks for reply.. i dont know....

FROM : https://maps.googleapis.com/maps/api/distancematrix/json?origins=Padova&destinations=Milano&language=it-IT&key=API_KEY

Noe Nazza
  • 61
  • 1
  • 10

2 Answers2

2

You can convert it to an array with json_decode like this:

<?php
$json = json_decode('{
   "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"
}',true);

var_dump($json);
?>

The result is:

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"
        }
      }
    }
  }
  ["status"]=>
  string(2) "OK"
}

In order to only get your km, you can do this afterwards:

<?php
    echo $json['rows']['0']['elements']['0']['distance']['text'];
?>
Nytrix
  • 1,139
  • 11
  • 23
1

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.

Tom Udding
  • 2,264
  • 3
  • 20
  • 30