簡體   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