I'm new to php and I'm attempting to work with json data and 2d arrays.
I have converted a json object from the google distance matrix API into an array. I would like to take the origin and destination address as the keys in a 2d matrix and then but the distances in the array accordingly.
In reality it would look something like:
origin1 origin2
dest1 2 3
dest2 10 1
I have the locations in separate variables too. I thought I could set these variables as the keys in the 2d array but this did not seem to work.
I think I should be able to do all this working only from the json that I converted to an array but I'm not sure how. As a result I maybe over complicating things.
Any help would be great. Thanks!
<?php
include_once('dbHandle.php');
$pcSet1 = getString();
$url="http://maps.googleapis.com/maps/api/distancematrix/json?origins=London,UK|Leeds,UK&destinations=London,UK|Leeds,UK&mode=driving&language=en-UK&UNITS=imperial&sensor=false";
$json = file_get_contents($url); // get the data from Google Maps API
$result = json_decode($json, true); // convert it from JSON to php array
$origin_addresses = array();
for($i=0; $i<sizeof($result); $i++){
$origin_addresses = $result['origin_addresses'];
}
$dist = array_flip($origin_addresses);
//2d array to look like this but with Origin replaced with location and distances from the json
$arr = array("origin1" => array("dest1" => "1 mile", "dest2"=>"5 miles", "dest3"=>"10 miles" ),
"origin2" => array("dest1" => "8 mile", "dest2"=>"6 miles", "dest3"=>"11 miles" )
);
?>