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

时间:2015-01-31 13:37:04

标签: php arrays json google-maps

我从数据库中获取地址和名称,并且我希望根据Google Maps 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'])和公里数,所有这些都按照kms按升序排序。为了达到这个目的,我应该如何看待?或者其他地方是否存在问题?

这是我的代码:

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

1 个答案:

答案 0 :(得分:0)

首先让我说这段代码很乱!看到从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/>";
}