I get the address and the name from the database and I want to sort the results based on the distance that is being calculated by Google Maps API after it gets the destination from a query. It currently displays the following error messages 4 times (repeating them):
Warning: Illegal string offset 'routes' in C:\xampp\htdocs\app2\test.php on line 36
Warning: Illegal string offset 'legs' in C:\xampp\htdocs\app2\test.php on line 36
Warning: Illegal string offset 'distance' in C:\xampp\htdocs\app2\test.php on line 36
Warning: Illegal string offset 'text' in C:\xampp\htdocs\app2\test.php on line 36
The printed array is displayed after the errors and it looks like this:
Array ( [0] => 0.3 km [1] => 1.7 km )
I want the result to be Name ($row['name'])
and number of kms, all sorted in an ascending order based on kms. How should my usort look in order to achieve this? Or is there a problem somewhere else?
Here's my code:
<?php
require_once 'includes/session.php';
require_once 'includes/config.php';
require_once 'includes/design_query.php';
include_once 'includes/header.php';
include_once 'includes/menu.php';
//$origin = $_COOKIE['origin'];
//$id=$_GET['id'];
$origin = "Bucuresti+Avrig+30";
$id = 3;
$query = ("SELECT * FROM locations WHERE category_id='$id'");
$result = mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_array($result))
{
$address = $row['Judet'] . '+' . $row['Localitate'] . '+' . $row['Strada'] . '+' . $row['Numar'];
$address = str_replace(' ', '+', $address);
$fullurl = 'http://maps.googleapis.com/maps/api/directions/json?origin=' . $origin . '&destination=' . $address . '&alternatives=true&sensor=true';
$json_a = json_decode((file_get_contents($fullurl)),true);
asort($row);
//echo $row['name'];
//echo "<span class='badge'>" . $json_a['routes'][0]['legs'][0]['distance']['text'] . "</span></button>";
$whole_data[] = $json_a['routes'][0]['legs'][0]['distance']['text'];
}
function cmp($a, $b)
{
if ($a['routes'][0]['legs'][0]['distance']['text'] == $b['routes'][0]['legs'][0]['distance']['text']) {
return 0;
}
return ($a['routes'][0]['legs'][0]['distance']['text'] < $b['routes'][0]['legs'][0]['distance']['text']) ? -1 : 1;
}
$a = array(3, 2, 5, 6, 1);
usort($whole_data, "cmp");
print_r($whole_data);
include_once 'includes/footer.php';
include_once 'includes/scripts.php';
?>