douwo8140 2015-01-31 13:37
浏览 72
已采纳

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

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';
?>
  • 写回答

1条回答 默认 最新

  • drwu24647 2015-01-31 13:52
    关注

    First of all let me just say that this code is a mess! Seeing the commented line where id is obtained from GET also makes it pretty clear that it will be prone to sql injection.

    Anyway here is an attempt to make it do what you want (just important parts):

    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/>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?