dto52236 2012-02-22 01:39
浏览 19
已采纳

按值排序关联数组并获得十个最大的键

I have an associative array from a loop where I store a user_id (key) and a score (value)

$scores[$user_id] = $score;

I was wondering, after this loop has finished, how can I sort the values (get the ten highest scores) and get their keys.

  • 写回答

3条回答 默认 最新

  • duanfuxing2417 2012-02-22 01:42
    关注
    asort($scores);
    $tenHighest = array_slice($scores, -10, null, true);
    $tenHighestKeys = array_keys($tenHighest);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?