如何对 php 中的数组进行排序
how to sort arrays in php
我需要先按优先级对 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);
但它是按字段升序排序的。这种排序基于我在这里找到的 post:PHP usort sorting multiple fields
我仍在尝试查看它以更好地了解它在做什么。我显然没有为我的用例正确调整它。
如有任何建议,我们将不胜感激。
谢谢。
我犯了一个愚蠢的错误。
我以为我做到了,但我错过了以下内容的交换:
return $a['weight'] - $b['weight'];
也就是现在:
return $b['weight'] - $a['weight'];
谢谢。
我需要先按优先级对 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);
但它是按字段升序排序的。这种排序基于我在这里找到的 post:PHP usort sorting multiple fields
我仍在尝试查看它以更好地了解它在做什么。我显然没有为我的用例正确调整它。
如有任何建议,我们将不胜感激。 谢谢。
我犯了一个愚蠢的错误。 我以为我做到了,但我错过了以下内容的交换:
return $a['weight'] - $b['weight'];
也就是现在:
return $b['weight'] - $a['weight'];
谢谢。