【问题标题】:how to sort arrays in php如何在php中对数组进行排序
【发布时间】:2015-04-12 03:03:52
【问题描述】:

我需要首先按优先级对 dns 查询的结果进行排序,然后在具有最大优先级值的记录中,我想按最低权重排序。 比如:

假设我从我的 dns_get_record() 查询中得到以下数据:

 { 
    [0]=> array(8) 
    { 
        ["host"]=> string(27) "_sip._udp.test.123.test.net" 
        ["class"]=> string(2) "IN" 
        ["ttl"]=> int(360) 
        ["type"]=> string(3) "SRV" 
        ["pri"]=> int(10) 
        ["weight"]=> int(100) 
        ["port"]=> int(5060) 
        ["target"]=> string(22) "mysip1.123.test.net" 
    } 
    [1]=> array(8) 
    { 
        ["host"]=> string(27) "_sip._udp.test.123.test.net" 
        ["class"]=> string(2) "IN" 
        ["ttl"]=> int(360) 
        ["type"]=> string(3) "SRV" 
        ["pri"]=> int(20) 
        ["weight"]=> int(100) 
        ["port"]=> int(5060) 
        ["target"]=> string(23) "mysip2.123.test.net" 
    } 
    [2]=> array(8) 
    { 
        ["host"]=> string(27) "_sip._udp.test.123.test.net" 
        ["class"]=> string(2) "IN" 
        ["ttl"]=> int(360) 
        ["type"]=> string(3) "SRV" 
        ["pri"]=> int(20) 
        ["weight"]=> int(200) 
        ["port"]=> int(5060) 
        ["target"]=> string(23) "mysip3.123.test.net" 
    } 
} 

我想要的结果是这样的:

 { 
    [0]=> array(8) 
    { 
        ["host"]=> string(27) "_sip._udp.test.123.test.net" 
        ["class"]=> string(2) "IN" 
        ["ttl"]=> int(360) 
        ["type"]=> string(3) "SRV" 
        ["pri"]=> int(20) 
        ["weight"]=> int(100) 
        ["port"]=> int(5060) 
        ["target"]=> string(23) "mysip2.123.test.net" 
    } 

    [1]=> array(8) 
    { 
        ["host"]=> string(27) "_sip._udp.test.123.test.net" 
        ["class"]=> string(2) "IN" 
        ["ttl"]=> int(360) 
        ["type"]=> string(3) "SRV" 
        ["pri"]=> int(20) 
        ["weight"]=> int(200) 
        ["port"]=> int(5060) 
        ["target"]=> string(23) "mysip3.123.test.net" 
    } 
    [2]=> array(8) 
    { 
        ["host"]=> string(27) "_sip._udp.test.123.test.net" 
        ["class"]=> string(2) "IN" 
        ["ttl"]=> int(360) 
        ["type"]=> string(3) "SRV" 
        ["pri"]=> int(10) 
        ["weight"]=> int(100) 
        ["port"]=> int(5060) 
        ["target"]=> string(22) "mysip1.123.test.net" 
    } 

} 

请注意,我现在拥有: 1. 优先级从大到小排序...... 2. 在优先级较高的记录中,权重值较小的记录在最前面。

代码

我已经编写了第一个排序的代码,其中具有较高优先级值的项目位于顶部。这是我的代码到目前为止的样子:

 $results=dns_get_record("test.123.test.net", DNS_NAPTR);
 $answer = $results[0]["replacement"];
 $sipservers = dns_get_record($answer,DNS_SRV);

然后进行排序:

    private function comp($a, $b) {
            if ($a['pri'] == $b['pri']) {
                    return $a['weight'] - $b['weight'];
            }
            return strcmp($a['pri'], $b['weight']);
    }

usort($sipservers, 'comp');
echo("<BR><BR> after sort: <BR><BR>");
var_dump($sipservers);

但它是按字段升序排序的。这种排序基于我在这里找到的一篇文章:PHP usort sorting multiple fields

我仍在尝试对此进行审查,以更好地了解它在做什么。我显然没有为我的用例适当地调整它。

任何建议将不胜感激。 谢谢。

【问题讨论】:

  • array_multisort 应该可以解决问题。
  • @AndreiP,您能否举例说明我如何更改当前对该功能的使用或再次使用它?
  • 啊,我想我只需要将 a-b 反转为 b-a。午饭后试试

标签: php sorting dns


【解决方案1】:

我犯了一个愚蠢的错误。 我以为我做到了,但我错过了以下交换:

return $a['weight'] - $b['weight'];

现在是:

return $b['weight'] - $a['weight'];

谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-02
    • 2010-12-04
    • 2014-03-04
    • 1970-01-01
    • 2017-03-20
    • 2011-07-26
    • 2010-10-10
    • 2011-02-11
    相关资源
    最近更新 更多