user3232285 user3232285 - 3 months ago 18
JSON Question

How to print the Google Distance Matrix Results Json Data

How to print the Results from Google Distance matrix iam Getting a results like this:

"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "4,8 km",
"value" : 4820
},
"duration" : {
"text" : "17 minutes",
"value" : 1038
},
"status" : "OK"
},
{
"distance" : {
"text" : "11,1 km",
"value" : 11064
},
"duration" : {
"text" : "25 minutes",
"value" : 1506
},
"status" : "OK"
}
]
}
],


And My Php Code to print The Output is

$json = file_get_contents($url);
$result = json_decode($json, true);
for($i=0; $i<count($result['rows']); $i++) {

echo "Kilometers " . $result['rows'][$i]["elements"][$i]['distance']['text'];

}


.Please Help

Answer

Given that you haven't included all the response, the method invlolved in the retrieval of the data nor the final desired output it is tricky to answer but I hope the following will give guidance on how to render the output.

This uses example data from the interwebs rather than the partial example code given in the question - a bit rough perhaps but...

$res='{
   "destination_addresses" : [
      "Commons Way, Bridgewater, NJ 08807, USA",
      "Morris Turnpike, Short Hills, NJ 07078, USA",
      "Monmouth Mall, Eatontown, NJ 07724, USA",
      "Garden State Plaza Blvd, Paramus, NJ 07652, USA",
      "Newport Centre Mall, Jersey City, NJ 07302, USA"
   ],
   "origin_addresses" : [ "75 Ninth Ave, New York, NY 10011, USA" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "68.8 km",
                  "value" : 68781
               },
               "duration" : {
                  "text" : "56 mins",
                  "value" : 3334
               },
               "duration_in_traffic" : {
                  "text" : "1 hour 1 min",
                  "value" : 3687
               },
               "status" : "OK"
            },
            {
               "distance" : {
                  "text" : "34.8 km",
                  "value" : 34806
               },
               "duration" : {
                  "text" : "36 mins",
                  "value" : 2138
               },
               "duration_in_traffic" : {
                  "text" : "41 mins",
                  "value" : 2487
               },
               "status" : "OK"
            },
            {
               "distance" : {
                  "text" : "86.3 km",
                  "value" : 86322
               },
               "duration" : {
                  "text" : "1 hour 6 mins",
                  "value" : 3930
               },
               "duration_in_traffic" : {
                  "text" : "1 hour 7 mins",
                  "value" : 4044
               },
               "status" : "OK"
            },
            {
               "distance" : {
                  "text" : "32.3 km",
                  "value" : 32278
               },
               "duration" : {
                  "text" : "33 mins",
                  "value" : 2009
               },
               "duration_in_traffic" : {
                  "text" : "35 mins",
                  "value" : 2082
               },
               "status" : "OK"
            },
            {
               "distance" : {
                  "text" : "6.9 km",
                  "value" : 6879
               },
               "duration" : {
                  "text" : "19 mins",
                  "value" : 1113
               },
               "duration_in_traffic" : {
                  "text" : "24 mins",
                  "value" : 1444
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}';

/* return as an object for easier notation */
$json=json_decode( $res, false );
$origins = $json->origin_addresses;
$destinations=$json->destination_addresses;
/*  if there are multiple rows, use a loop and `$rows[$i]` etc */
$elements=$json->rows[0]->elements;
foreach( $elements as $i => $obj ){

    echo '
    <div>
        <h5>From: '.$origins[ 0 ].' to '.$destinations[ $i ].'</h5>
        Distance: '.$obj->distance->text.',
        Duration:'.$obj->duration->text.',
        In traffic: '.$obj->duration_in_traffic->text.',
        Status: '.$obj->status.'
    </div>';
}
Comments