douhuang75397
2013-09-06 15:54
采纳率: 0%
浏览 38
已采纳

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

After calculating distances between two points using latitude and longitude, I've created an array which looks like this:

$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'));

}

The main array contains the enterprises that is needed for a comparison with the actual postal codes inside it. POSTAL CODE => DISTANCE.

I need to sort those inner arrays by distance from the nearest to the farthest and I don't really understand how array_multisort works...

图片转代码服务由CSDN问答提供 功能建议

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

  $ enterprises = array(); 
 
 // Ex。:Array([0] =&gt;数组([A0A0A0] =&gt; 0)[1] =  &gt;数组([A0A1A0] =&gt; 22.794344863539)
for($ i = 0; $ i&lt; count($ cpEnterprise); $ i ++){
 
 $ enterprises [] = array($ cpEnterprise [$ i  ] =&gt;距离($ actualCpLat,$ actualCpLong,$ businessLat [$ i],$ businessLong [$ i],'k')); 
 
} 
   
 \  n 

主数组包含与其中的实际邮政编码进行比较所需的企业.POSTAL CODE =&gt; DISTANCE。

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongtaoxue4674 2013-09-06 16:08
    已采纳

    An easy way to tackle this is to restructure your array and use 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);
    
    点赞 打赏 评论
  • dongyuan2388 2013-09-06 16:10

    It depends on your sort situation to use array_multisort. I'll give my example, you might get some clues:

    $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);
    

    Result:

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

    This is just an example on array_multisort and defiantly there are more better solutions and answers to your question.

    点赞 打赏 评论

相关推荐 更多相似问题