简体   繁体   English

按特定键对多维数组进行排序

[英]Sorting Multidimensional Array by Specific Key

EDIT : For anyone who might come across this post with a similar problem, It was solved by taking konforce 's supplied answer and tweaking around a bit with the custom sorting function:编辑:对于任何可能遇到类似问题的帖子的人,通过采用konforce提供的答案并使用自定义排序 function 进行一些调整来解决它:

function cmp($a, $b) {
    if ($a[5] == $b[5]) {
        return ($a[3] < $b[3]) ? -1 :1;
    }
    return ($a[5] > $b[5]) ? -1 : 1;
}

Notice $a[5] == $b[5] does not return zero.注意$a[5] == $b[5]不返回零。 It was changed to check who has the most losses and then sort it in ASC order.它已更改为检查谁损失最多,然后按 ASC 顺序对其进行排序。 I'm sure you can even keep going and add another if-statement in there in-case they have the same losses.我相信你甚至可以继续并在那里添加另一个 if 语句,以防他们有相同的损失。

Lastly, all you do is usort($ARRAY, "cmp");最后,你要做的就是usort($ARRAY, "cmp"); and finito!!!和finito!

Original Post原帖

My apologies for coming up with yet another MD Array sorting question but I'm just not getting it.我很抱歉提出另一个 MD 数组排序问题,但我就是不明白。 I've searched aplenty for a solution and although many sites have provided what seemed like a logical answer I still have not been able to figure it out.我已经搜索了很多解决方案,尽管许多网站都提供了看似合乎逻辑的答案,但我仍然无法弄清楚。

My problem is since I'm still learning its been rather difficult for me to grasp the concept of using usort with a custom comparing function.我的问题是,因为我仍在学习,所以很难掌握使用 usort 和自定义比较 function 的概念。 Atleast, thats what I have seen the most when others have tried to sort MD Arrays.至少,这是我在其他人尝试对 MD Arrays 进行排序时看到的最多的。

I'm working on a small project to sharpen up on my php skills.我正在做一个小项目来提高我的 php 技能。 Its a very basic tournament standings script that holds a team's information within an array.它是一个非常基本的锦标赛排名脚本,将球队的信息保存在一个数组中。 I would like to sort the array by most points($array[X][X][5]).我想按大多数点对数组进行排序($array[X][X][5])。

So the array looks something like this:所以数组看起来像这样:

Array (
        [0] => Array (
            [0] => Array (
                [0] => cooller
                [1] => 6 
                [2] => 6 
                [3] => 0 
                [4] => 0 
                [5] => 18 
            )
        ) 
        [1] => Array (
            [0] => Array (
                [0] => strenx 
                [1] => 9 
                [2] => 5 
                [3] => 1 
                [4] => 3 
                [5] => 18
            )
        )
        [2] => Array (
            [0] => Array (
                [0] => rapha
                [1] => 10
                [2] => 8
                [3] => 1
                [4] => 1
                [5] => 25
            )
        ) [3] => Array (
            [0] => Array (
                [0] => ronald reagan
                [1] => 5 
                [2] => 4 
                [3] => 0 
                [4] => 1 
                [5] => 13
            )
        )
    )

I would like to sort it by most points(cell #5), so it would look like this after sorting:我想按大多数点(单元格#5)对其进行排序,因此排序看起来像这样:

Array (
        [0] => Array (
            [0] => Array (
                [0] => rapha
                [1] => 10
                [2] => 8
                [3] => 1
                [4] => 1
                [5] => 25
            )
        )
        [1] => Array (
            [0] => Array (
                [0] => cooller
                [1] => 6 
                [2] => 6 
                [3] => 0 
                [4] => 0 
                [5] => 18 
            )
        ) 
        [2] => Array (
            [0] => Array (
                [0] => strenx 
                [1] => 9 
                [2] => 5 
                [3] => 1 
                [4] => 3 
                [5] => 18
            )
        )
        [3] => Array (
            [0] => Array (
                [0] => ronald reagan
                [1] => 5 
                [2] => 4 
                [3] => 0 
                [4] => 1 
                [5] => 13
            )
        )
    )

The player with 25 points would be at the top, followed by 18, 18, and lastly 13. Sorry for my earlier post, was having difficulty wording my question correctly.获得 25 分的玩家将位居榜首,其次是 18、18,最后是 13。抱歉我之前的帖子,我的问题难以正确表达。 Thanks in advanced!先谢谢了!

I think you want something like this:我想你想要这样的东西:

usort($standings, function($a, $b) { return $b[0][5] - $a[0][5]; });

Or prior to PHP 5.3:或在 PHP 5.3 之前:

function cmp($a, $b) { return $b[0][5] - $a[0][5]; }
usort($standings, 'cmp');

When using usort , the $a and $b parameters will be one "layer" into the supplied array.使用usort时, $a$b参数将是提供的数组中的一个“层”。 So in your case, an example of $a or $b will be:因此,在您的情况下, $a$b的示例将是:

[0] => Array (
         [0] => cooller
         [1] => 6 
         [2] => 6 
         [3] => 0 
         [4] => 0 
         [5] => 18 
)

I'm not sure why you have an extra containing array there, but as you can see, you want to sort based on the [0][5] position.我不确定为什么你有一个额外的包含数组,但正如你所看到的,你想根据[0][5] position 进行排序。

usort($standings[0][0][5], 'cmp') won't work because the first parameter isn't an array to sort, it's just a single number, the points. usort($standings[0][0][5], 'cmp')不起作用,因为第一个参数不是要排序的数组,它只是一个数字,即点数。

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

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