douyan1903 2017-10-04 17:56
浏览 31
已采纳

php多维数组按键排序,但有异常

I am trying to sort a multidimensional array by its keys with exceptions to the sort. I know that I can use array_multisort(array_column ... to sort by multiple columns but my issue is that my sort is more by type than it is by alphanumeric or numerical.

An example of the multidimensional array that I am working with is below:

[
 0 ['woid'=>17642-1,'schedule'=>'URGENT']
 1 ['woid'=>17643-1,'schedule'=>'ASAP']
 2 ['woid'=>17643-2,'schedule'=>'ASAP']
 3 ['woid'=>17644-1,'schedule'=>'JAN']
 4 ['woid'=>NC323-1,'schedule'=>'URGENT']
 5 ['woid'=>NC324-1,'schedule'=>'ASAP']
]

What I want is to have this sorted by key=woid starting with 'NC' at the top sorted ASC, then sort the remaining by key=schedule with the following order:

URGENT
JAN
FEB
MAR
APR
MAY
JUN
JUL
AUG
SEP
OCT
NOV
DEC
ASAP

all while retaining an ASC sort on the key=woid

I understand this may be confusing. Below is an example of the desired outcome with the array example above:

[
 0 ['woid'=>NC323-1,'schedule'=>'URGENT']
 1 ['woid'=>NC324-1,'schedule'=>'ASAP']
 2 ['woid'=>17642-1,'schedule'=>'URGENT']
 3 ['woid'=>17644-1,'schedule'=>'JAN']
 4 ['woid'=>17643-1,'schedule'=>'ASAP']
 5 ['woid'=>17643-2,'schedule'=>'ASAP']
]

If more information is needed or a larger example I am more than happy to take the time and provide it.

  • 写回答

1条回答 默认 最新

  • dozrhldy285751 2017-10-04 18:09
    关注

    You'll need to use usort for multiple sort criteria like this.

    For your usort comparison function, after you've figured out whether each woid starts with 'NC', you can compare the results of that check mathematically, then use the order from your custom sort array, and finally use strcmp to break ties.

    $order = ['URGENT','JAN','FEB','MAR','APR','MAY','JUN',
              'JUL','AUG','SEP','OCT','NOV','DEC','ASAP'];
    
    $order = array_flip($order);
    
    usort($array, function($a, $b)  use ($order) {
        $aNC = strpos($a['woid'], 'NC') === 0;
        $bNC = strpos($b['woid'], 'NC') === 0;
        return ($bNC - $aNC)
            ?: ($order[$a['schedule']] - $order[$b['schedule']])
            ?: strcmp($a['woid'], $b['woid']);
    });
    

    In the first comparison, $aNC and $bNC will be booleans, but will be converted to ints (0 or 1) for the subtraction. If that comparison returns 0 (meaning both or neither woid value starts with 'NC'), the second comparison will be evaluated.

    In the second comparison, the position of the two schedule values in the $order array is subtracted. If that comparison returns 0, (meaning both schedule values are the same), the third comparison will be evaluated.

    In the third comparison, strcmp is used to return <0, 0, or >0 depending on the string comparison of the two woid values.

    Working Demo at 3v4l.org

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题