Php usort正在以相反的顺序返回数组
这个usort函数返回的数组与我想要的相反。它返回一个类似于(“1”、“2”、“3”)的数组。我怎样才能使它返回(“3”、“2”、“1”)Php usort正在以相反的顺序返回数组,php,Php,这个usort函数返回的数组与我想要的相反。它返回一个类似于(“1”、“2”、“3”)的数组。我怎样才能使它返回(“3”、“2”、“1”) 只要颠倒参数就行了 usort($myArray, function($a, $b) { return $b["comments"] - $a["comments"]; }); usort($myArray, function($a, $b) { return $b["comments"] - $a["comments"]; }); 您可
只要颠倒参数就行了
usort($myArray, function($a, $b) {
return $b["comments"] - $a["comments"];
});
usort($myArray, function($a, $b) {
return $b["comments"] - $a["comments"];
});
您可以反转您的函数输出 只需将A更改为B,将B更改为A。
usort($myArray,function($A,$B){
如果($a['comments']==$b['comments'])){
返回0;
}
返回($a['comments']>$b['comments'])?-1:1;
});
返回$b[“评论”]-$a[“评论”]代码>请在你的答案中添加一些解释,以便其他人可以从中学习。你可以使用太空船操作员和箭头功能。你为什么把这件事弄得这么复杂?@Dharman这与官方文档中的例子类似。我认为这不是一个正确的答案。如果$myArray
尚未排序怎么办?
usort($myArray, function($a, $b) {
return $b["comments"] - $a["comments"];
});
$myArray = array("1", "2", "3");
$reversed_array = array_reverse($myArray);
usort($myArray, function($a, $b) {
return $b["comments"] - $a["comments"];
});