简体   繁体   English

对具有某些已知值的数组进行优先排序,php

[英]Sorting an array with certain known values prioritized, php

I want to sort an array based on a sub array value, that part is fairly simple, but there is an additional requirement.我想根据子数组值对数组进行排序,这部分相当简单,但还有一个额外的要求。

I also have a known set of values, in which case I want to sort by those instead我也有一组已知的值,在这种情况下我想按这些值排序

In this case the values I am sorting by are IP addresses.在这种情况下,我排序的值是 IP 地址。

For example, if the address is 1.1.1.5, I want that address to be the first value in the list, and if its 1.1.7.3, I want that to be the second value on the list, and so on.例如,如果地址是 1.1.1.5,我希望该地址是列表中的第一个值,如果是 1.1.7.3,我希望它是列表中的第二个值,依此类推。

Then once I am out of known addresses, I want the rest to simply be sorted normally.然后,一旦我没有已知地址,我希望 rest 能够简单地正常排序。

The starting order of the array is completely random If possible I also want to append something to the front of the known IPs数组的起始顺序是完全随机的如果可能的话我还想把 append 放在已知 IP 的前面

How I sort it without the prioritization:我如何在没有优先级的情况下对其进行排序:

uasort($array, function func($a, $b) {
    return strcmp($a[4], $b[4]);
});

I do not think you can change the IP value within the sort so that would need to be done afterwards.我不认为您可以更改排序中的 IP 值,以便之后需要完成。 How about something like this though for the sort function (you would need to test performance):对于 function 这样的东西怎么样(你需要测试性能):

$knownIPs = array(
    '1.1.1.5' => 1,
    '1.1.7.3' => 2,
);

$array = array(
    array('f', '0.0.0.0'),
    array('t', '1.1.7.3'),
    array('w', '1.1.1.5'),
    array('r', '1.1.1.6'),
    array('c', '1.1.1.7'),
    array('a', '1.1.1.8'),
    array('i', '1.1.1.9'),
);

uasort($array, function ($a, $b) {
  global $knownIPs;

  $knownIPsKeys = array_keys($knownIPs);
  //if a is a known IP then it has to be before any unknown IP
  if(in_array($a[1],$knownIPsKeys) && !in_array($b[1],$knownIPsKeys)){
    return -1;
  }
  //if b is a known IP then it has to be before any unknown IP
  if(in_array($b[1],$knownIPsKeys) && !in_array($a[1],$knownIPsKeys)){
    return 1;
  }
  //if a and b are both known then check if their order is the same or different
  if(in_array($a[1],$knownIPsKeys) && in_array($b[1],$knownIPsKeys)){
    //if a and b have the same order then compare on the other field as per default
    if($knownIPs[$a[1]]===$knownIPs[$b[1]]){
      return strcmp($a[0], $b[0]);
    }
    //otherwise compare on the order value
    return $knownIPs[$a[1]]<$knownIPs[$b[1]]?-1:1;
  }
  //compare on other field as default
  return strcmp($a[0], $b[0]);
});

print_r($array);

This is a simple script using a global variable to hold the known IPs, there are a number of different ways that $knownIPs can be set up and used depending obviously on the rest of your code but hopefully this gives an idea of how to achieve the sorting.这是一个使用全局变量保存已知 IP 的简单脚本,可以通过多种不同的方式设置和使用 $knownIPs,这显然取决于代码的 rest,但希望这可以让您了解如何实现排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM