在PHP

时间:2016-07-19 15:18:12

标签: php sorting multidimensional-array

我正在尝试按分数

对以下数组进行排序
$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));

这不起作用,我找不到任何有用的文档或示例。我尝试过使用array_multisort()usort(),但没有成功。

score降序排列此数组的干净方法是什么?

1 个答案:

答案 0 :(得分:4)

如果您只对一个维度感兴趣,则无需使用多重排序。这样做是为了按分数的降序排序:

usort($questionsets,function($a,$b){return $b['score']-$a['score'];});

Live demo