PHP - array_multisort?邮政编码排序(邮政编码)

时间:2013-09-06 15:54:38

标签: php postal-code array-multisort

使用纬度和经度计算两点之间的距离后,我创建了一个如下所示的数组:

$enterprises = array();

//Ex.: Array ( [0] => Array ( [A0A0A0] => 0 ) [1] => Array ( [A0A1A0] => 22.794344863539 ) 
for ($i=0; $i < count($cpEnterprise) ; $i++) {

        $enterprises[] = array($cpEnterprise[$i] => distance($actualCpLat, $actualCpLong, $businessLat[$i], $businessLong[$i], 'k'));

}

主阵列包含与其中的实际邮政编码进行比较所需的企业。邮政编码=&gt;距离。

我需要按距离最近到最远的距离对这些内部数组进行排序,我真的不明白array_multisort是如何工作的......

2 个答案:

答案 0 :(得分:3)

解决这个问题的一个简单方法是重组数组并使用asort

$enterprises = array();

//Ex.: Array ( [0] => Array ( [A0A0A0] => 0 ) [1] => Array ( [A0A1A0] => 22.794344863539 ) 
for ($i=0; $i < count($cpEnterprise) ; $i++) {    
        $enterprises[$cpEnterprise[$i]] = distance($actualCpLat, $actualCpLong, $businessLat[$i], $businessLong[$i], 'k');

}
asort($enterprises);

答案 1 :(得分:1)

使用array_multisort取决于您的排序情况。我举一个例子,你可能会得到一些线索:

$products_count = array(
  2 => 10,
  5 => 20,
  0 => 13
)

$counts = array();

foreach($products_count as $k => $v)
{
  $counts[$k] = $v;
}

array_multisort($counts, SORT_NUMERIC, SORT_ASC, $products_count);

结果:

array(
  0 => 13,
  2 => 10,
  5 => 20
)

这只是array_multisort的一个例子,而且你的问题有更好的解决方案和答案。