繁体   English   中英

在PHP中排序多维关联数组

[英]Sort multidimensional associative array in PHP

我正在尝试按分数对以下数组进行排序

$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按降序对该数组排序的干净方法是什么?

如果您只对一个维度感兴趣,则无需使用多分类。 而是按分数降序排序:

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

现场演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM