[英]Sorting PHP array by 3 values
I need to sort an array by three values. 我需要按三个值对数组进行排序。 Here's a basic setup of how the array is setup:
这是数组设置的基本设置:
$arr = array(
'1' => array(
'start' => '1234',
'mh' => '12',
'status' => '1'
),
'2' => array(
'start' => '9874',
'mh' => '3',
'status' => '9'
),
'3' => array(
'start' => '5678',
'mh' => '6',
'status' => '2'
)
);
Currently, I've only had to sort by 2 values, which array_multisort came in handy for. 目前,我只需要按2个值进行排序,array_multisort就派上用场了。 Now I need to sort by all three values in this order: Status (low) -> Start (low) -> MH (high).
现在,我需要按以下三个值排序:状态(低)->启动(低)-> MH(高)。 Meaning that the lowest status is first, then the lowest start, then the highest MH.
这意味着最低状态是第一,然后是最低启动,然后是最高MH。
Any help would be appreciated. 任何帮助,将不胜感激。
A general solution for sorting by multiple columns: 按多列排序的一般解决方案:
usort($arr,function($a,$b) {
return ($a['status'] - $b['status']) // status ascending
?: ($a['start'] - $b['start']) // start ascending
?: ($b['mh'] - $a['mh']) // mh descending
;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.