按多个字段排序数组

时间:2018-01-04 03:07:17

标签: php arrays array-multisort

在我的后端,我有一些代码处理团队参加循环赛团队比赛,最后团队以数组形式返回并包含比赛结果/统计数据等。

我有以下数组,我想按以下顺序按两个字段排序:points(desc),round_difference(desc)。

最终正确的顺序是:TeamOneTeamThreeTeamTwo

我将如何使用此格式的数组进行此操作?有可能吗?我调查了array_multisort()但是我的数组键/结构整体上有点"奇怪" 与我在页面上找到的其他代码示例相比,并且非常令人困惑对我来说。

希望有人可以帮助我。

Array
(
    [1] => Array
        (
            [1] => Array
                (
                    [id] => 1
                    [group] => 1
                    [name] => TeamOne
                    [tag] => One
                    [matches] => Array
                        (
                            [played] => 1
                            [won] => 0
                            [ot_won] => 1
                            [ot_lost] => 0
                            [lost] => 0
                            [round_difference] => 3
                            [points] => 2
                        )

                )

            [238] => Array
                (
                    [id] => 238
                    [group] => 1
                    [name] => TeamTwo
                    [tag] => Two
                    [matches] => Array
                        (
                            [played] => 0
                            [won] => 0
                            [ot_won] => 0
                            [ot_lost] => 0
                            [lost] => 0
                            [round_difference] => 0
                            [points] => 0
                        )

                )

            [14] => Array
                (
                    [id] => 14
                    [group] => 1
                    [name] => TeamThree
                    [tag] => Three
                    [matches] => Array
                        (
                            [played] => 1
                            [won] => 0
                            [ot_won] => 0
                            [ot_lost] => 1
                            [lost] => 0
                            [round_difference] => -3
                            [points] => 1
                        )
                )
        )

1 个答案:

答案 0 :(得分:0)

请尝试使用uasort()并比较这些键:

uasort($arr[1],function($a,$b){
    # If the points are more/less right off the bat, return results
    if($a['matches']['points'] < $b['matches']['points'])
        return true;
    # If they are the same
    elseif($a['matches']['points'] == $b['matches']['points'])
        # Compare round_difference sort by that
        return ($a['matches']['round_difference'] < $b['matches']['round_difference']);
});

所以,排序如下:

$arr    =   array(
    1 => array(
        1 => array(
                'id' => 1,
                'group' => 1,
                'name' => 'TeamOne',
                'tag' => 'One',
                'matches' => array(
                        'played' => 1,
                        'won' => 0,
                        'ot_won' => 1,
                        'ot_lost' => 0,
                        'lost' => 0,
                        'round_difference' => 3,
                        'points' => 2
                    )

            ),
        238 => array
            (
                'id' => 238,
                'group' => 1,
                'name' => 'TeamTwo',
                'tag' => 'Two',
                'matches' => array(
                        'played' => 0,
                        'won' => 0,
                        'ot_won' => 0,
                        'ot_lost' => 0,
                        'lost' => 0,
                        'round_difference' => 0,
                        'points' => 0,
                    )

            ),
        14 => array
            (
                'id' => 14,
                'group' => 1,
                'name' => 'TeamThree',
                'tag' => 'Three',
                'matches' => array
                    (
                        'played' => 1,
                        'won' => 0,
                        'ot_won' => 0,
                        'ot_lost' => 1,
                        'lost' => 0,
                        'round_difference' => '-3',
                        'points' => 1,
                    )

            ),
        17 => array
            (
                'id' => 17,
                'group' => 1,
                'name' => 'TeamFive',
                'tag' => 'Five',
                'matches' => array
                    (
                        'played' => 1,
                        'won' => 0,
                        'ot_won' => 0,
                        'ot_lost' => 1,
                        'lost' => 0,
                        'round_difference' => '-3',
                        'points' => 2,
                    )

            )
        )
    );

给你:

Array
(
    [1] => Array
        (
            [1] => Array
                (
                    [id] => 1
                    [group] => 1
                    [name] => TeamOne
                    [tag] => One
                    [matches] => Array
                        (
                            [played] => 1
                            [won] => 0
                            [ot_won] => 1
                            [ot_lost] => 0
                            [lost] => 0
                            [round_difference] => 3
                            [points] => 2
                        )

                )

            [17] => Array
                (
                    [id] => 17
                    [group] => 1
                    [name] => TeamFive
                    [tag] => Five
                    [matches] => Array
                        (
                            [played] => 1
                            [won] => 0
                            [ot_won] => 0
                            [ot_lost] => 1
                            [lost] => 0
                            [round_difference] => -3
                            [points] => 2
                        )

                )

            [14] => Array
                (
                    [id] => 14
                    [group] => 1
                    [name] => TeamThree
                    [tag] => Three
                    [matches] => Array
                        (
                            [played] => 1
                            [won] => 0
                            [ot_won] => 0
                            [ot_lost] => 1
                            [lost] => 0
                            [round_difference] => -3
                            [points] => 1
                        )

                )

            [238] => Array
                (
                    [id] => 238
                    [group] => 1
                    [name] => TeamTwo
                    [tag] => Two
                    [matches] => Array
                        (
                            [played] => 0
                            [won] => 0
                            [ot_won] => 0
                            [ot_lost] => 0
                            [lost] => 0
                            [round_difference] => 0
                            [points] => 0
                        )

                )

        )

)