使用 usort 和 spaceship 运算符正确排序多维数组 [英] Properly sorting multidimensional array using usort and spaceship operator

查看:46
本文介绍了使用 usort 和 spaceship 运算符正确排序多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用以下逻辑对以下数组进行排序:如果分数"相同,我想使用时间"进行比较.数组如下:

I need to sort the following array with following logic: If the 'score' is the same, I want to compare using 'time'. The array is as follows:

$user_scores = [ 82 => [ 'score' => 1, 'time' => 6.442 ],
                 34 => [ 'score' => 1, 'time' => 5.646 ],
                 66 => [ 'score' => 3, 'time' => 1.554 ]
               ]

上述数组中的键"是user_ids",我需要将其保留在排序数组中.到目前为止我最好的尝试如下 -

The 'keys' in the above array are the 'user_ids', which I need to preserve in the sorted array. My best attempt so far is as below -

$result = usort( $user_scores, function ( $a, $b ) {
    if ( $a['score'] == $b['score'] ) {
        return $a['time'] == $b['time'];
        }
    return $a['score'] <=> $b['score'];
} );

显然,这是行不通的,我得到了 $user_scores 数组,其中所有键都替换为 0, 1, 2 而不是 user_ids ( 82, 34 和 66 ).排序也不起作用.

Obviously, this isn't working, and I'm getting the $user_scores array with all keys replaced with 0, 1, 2 instead of user_ids ( 82, 34 and 66 ). The sorting ain't working either.

对于上面的数组,我想要的输出是 $user_scores 数组:

For the above array, my desired output would be $user_scores array :

$user_scores = [ 34 => [ 'score' => 1, 'time' => 5.646 ],
                 82 => [ 'score' => 1, 'time' => 6.442 ],
                 66 => [ 'score' => 3, 'time' => 1.554 ]
               ]

如果您能告诉我如何使用飞船操作员进行这项工作(如果有意义的话),我将不胜感激.感谢您抽出宝贵时间,期待您的回复.

Would really appreciate if you could tell me how to make this work using the spaceship operator (if it makes sense). Thank you for your time and I look forward to your responses.

---更新---

所需的排序逻辑是这样的:

The sorting logic required is like this:

  1. 分数越高,排名越高.
  2. 时间越高,排名越低.

它基本上是对测验的结果进行排序.时间最少的得分最高的将排在前列;而那些得分较低和时间较长的将处于底部.

It's basically sorting the results of quiz. The top scorers with the least amount of time would be at the top; and those with lower score and higher time would be at the bottom.

推荐答案

要保留密钥,您只需要使用 uasort().

To preserve the keys, you just need to use uasort().

我建议飞船操作员进行 3 向比较(可用于 PHP7 及更高版本).

I recommend the spaceship operator to make the 3-way comparisons (available from PHP7 and higher).

$result 在你的代码中只会返回 true/false 所以这对你没用.sort() 函数不能分配给变量;他们直接修改输入数组.

$result in your code is only going to return true/false so that is no use to you. The sort()'ing functions aren't to be assigned to a variable; they directly modify the input array.

当向飞船操作员提供数组数据时,将比较前两个元素.如果有差异,则返回 1-1.如果有平局(比较计算结果为 0,那么接下来的两个元素将被计算.这种行为一直持续到发生非零计算或没有更多元素要迭代.这是一个相当要实现的干净/可读的语法.

When offering array data to the spaceship operator, the first two elements will be compared. If there is a difference, then 1 or -1 will be returned. If there is a tie (comparison evaluates as 0, then the next two elements will be evaluated. This behavior continues until a non-zero evaluation occurs or there are no more elements to iterate. This is a rather clean/readable syntax to implement.

要按降序排序,请在左侧写入 $b 值,在右侧写入 $a 值.

To sort in a descending direction, write the $b value on the left and the $a value on the right.

代码:(演示)

$user_scores = [
    82 => ['score' => 1, 'time' => 6.442],
    34 => ['score' => 1, 'time' => 5.646],
    66 => ['score' => 3, 'time' => 1.554],
    7  => ['score' => 2, 'time' => 4.442],
    99 => ['score' => 4, 'time' => 3.646],
    55 => ['score' => 1, 'time' => 2.554]
];

uasort($user_scores, function($a, $b) {
    return [$b['score'], $a['time']] <=> [$a['score'], $b['time']];
});
var_export($user_scores);

输出:

array (
  99 => 
  array (
    'score' => 4,
    'time' => 3.646,
  ),
  66 => 
  array (
    'score' => 3,
    'time' => 1.554,
  ),
  7 => 
  array (
    'score' => 2,
    'time' => 4.442,
  ),
  55 => 
  array (
    'score' => 1,
    'time' => 2.554,
  ),
  34 => 
  array (
    'score' => 1,
    'time' => 5.646,
  ),
  82 => 
  array (
    'score' => 1,
    'time' => 6.442,
  ),
)

这篇关于使用 usort 和 spaceship 运算符正确排序多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆