-3

我有以下数组:

$franchise_a_status[] = array(
    'id'   => $franchise['franchise_id'],
    'text' => $franchise['franchise_surname']. ' ' .$franchise['franchise_firstname'].' '.'('.$distance.')'
);

$franchise 数组是从数据库中填充的,距离变量从Google 距离矩阵中检索信息。我希望数组按距离顺序排序 - 从最近到最远。

我认为这很容易:

asort($franchise_a_status);

可悲的是我错了。我将如何根据距离对该数组进行排序?

4

2 回答 2

1

我只给你一个提示:使用usort()可以帮助你。

您应该编写比较函数。从那个长字符串中获取那个距离并不是一件容易的事,但是出于某种原因,你以这种方式存储它。

于 2012-07-05T15:54:10.967 回答
0

在这里使用asort对您没有帮助。我建议重组你的数组并使用ksort

未经测试的例子:

// some loop {

    $franchise_a_status[$distance . '_' . $franchise['franchise_id']] = array(
        'id'   => $franchise['franchise_id'],
        'text' => $franchise['franchise_surname'] . ' ' . $franchise['franchise_firstname'] . ' ' . '(' . $distance . ')'
    );

// }

ksort($franchise_a_status);

ksort将按键排序数组。通过将距离放在键的开头,结果将按距离排序。当距离恰好相同时,还需要 id 来避免过度编写特许经营权。

于 2012-07-05T15:57:40.920 回答