I'm trying to sort the following array by score
$questionsets = array(
"A" => array("category" => "Some Category A", "score" => 0),
"B" => array("category" => "Some Category B", "score" => 29),
"C" => array("category" => "Some Category C", "score" => 12),
"D" => array("category" => "Some Category D", "score" => 88),
"E" => array("category" => "Some Category E", "score" => 4),
"F" => array("category" => "Some Category F", "score" => 22),
"G" => array("category" => "Some Category G", "score" => 20),
"H" => array("category" => "Some Category H", "score" => 40),
"I" => array("category" => "Some Category I", "score" => 42)
);
$questionsets = array_msort($questionsets, array('score'=>SORT_DESC));
This doesn't work and I'm not finding any documentation or examples that help. I've tried using array_multisort()
and usort()
but with no success.
What is the clean way to sort this array by score
in descending order?
No need to use multi-sort if you're only interested in one dimension. Do this instead to sort in descending order of score:
usort($questionsets,function($a,$b){return $b['score']-$a['score'];});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.