合并4个数组后,我使用usort通过Date对最终数组进行排序.然而,它以升序的方式排序,我需要它是Descending.当只有2个数组被合并时,我已经看到了降序排序的解决方案,但是没有超过2个的例子.
每个数组($ files,$ newfiles,$ comments和$ ratings)都有以下字段:ID,Title,Date和Type.我需要通过Date Descending对它们进行排序.这是我目前的代码:
function dateSort($a,$b,$c,$d){
$dateA = strtotime($a['Date']);
$dateB = strtotime($b['Date']);
$dateC = strtotime($c['Date']);
$dateD = strtotime($d['Date']);
return ($dateA-$dateB-$dateC-$dateD);
//I HAVE ALSO TRIED REVERSING THE ORDER LIKE THIS BUT NO LUCK.
//THE SORTING IS STRANGE AND UN ORDERED WHEN I DO THIS:
// return ($dateD-$dateC-$dateB-$dateA);
}
$result = array_merge($files, $newfiles,$comments,$ratings);
usort($result, 'dateSort');
Run Code Online (Sandbox Code Playgroud)
$descending_array = array_reverse(usort($result, 'dateSort'));
Run Code Online (Sandbox Code Playgroud)
这会使数组反转,从而将升序反转为下降.