Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php usort正在以相反的顺序返回数组_Php - Fatal编程技术网

Php usort正在以相反的顺序返回数组

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函数返回的数组与我想要的相反。它返回一个类似于(“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"];
});

您可以反转您的函数输出

只需将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"];
});