2013-09-01 28 views
5

此usort函數正在從我想要的數組中返回數組。它返回一個數組,如(「1」,「2」,「3」)。我怎樣才能讓它返回(「3」,「2」,「1」)?usort正在以相反的順序返回數組

usort($myArray, function($a, $b) { 
    return $a["comments"] - $b["comments"]; 
}); 
+0

'回報$ B [ 「意見」] - $ A [ 「意見」];' –

回答

19

只是顛倒參數?

usort($myArray, function($a, $b) { 
    return $b["comments"] - $a["comments"]; 
}); 
1

你可以改變你的功能輸出。

usort($myArray, function($a, $b) { 
    return $b["comments"] - $a["comments"]; 
}); 
1
$myArray = array("1", "2", "3"); 
$reversed_array = array_reverse($myArray); 
2
usort($myArray, function($a, $b) { 
    return $b["comments"] - $a["comments"]; 
}); 

只要改變A到B和B到A

+0

謝謝哥們@Jelle – Mubin

相關問題