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 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧