根据 Google API 距离 (usort) 从数据库中对数组进行排序


Sorting an array from DB based on Google API distance (usort)

我从数据库中获取地址和名称,我想根据谷歌地图API从查询中获取目的地后计算的距离对结果进行排序。它当前显示以下错误消息 4 次(重复):

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 

打印的数组显示在错误之后,如下所示:

Array ( [0] => 0.3 km [1] => 1.7 km ) 

我希望结果是名称($row['name'])和公里数,所有这些都根据公里按升序排序。为了实现这一目标,我的 uclass 应该是什么样子?还是其他地方有问题?

这是我的代码:

<?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';
?>

首先让我说这段代码是一团糟!看到从GET获取id的注释行也很清楚,它很容易被SQL注入。

无论如何,这是尝试让它做你想做的事(只是重要的部分):

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); 

$whole_data[] = array("name" => $row["name"], "value" => $json_a['routes'][0]['legs'][0]['distance']['value'],  "text" => $json_a['routes'][0]['legs'][0]['distance']['text']);
}
 function cmp($a, $b)
    {
        if ($a['value'] == $b['value']) {
            return 0;
        }
        return ($a['value'] < $b['value']) ? -1 : 1;
    }

    usort($whole_data, "cmp");
// example print
foreach($whole_data as $row){
    echo "$row[name] $row[text] <br/>";
}