在PHP中对具有多个值的多维数组进行排序

时间:2015-08-28 12:06:57

标签: php sorting

如果两个P相同,我想按值P对数组进行排序,如果两个PD相同则按PD排序,然后按PF与PA相同排序。
我是如何实现这一目标的?

我在PHP中有多维数组

PHP:

$abc = array(
    array('id' => 1,'name' => 'abc','overall' => array('MP' => '2','W'=> '2','L' => '0','D'=> '0','PF' => '2904','PA' => '1932','PD' => '972','P' => '6')),
    array('id' => 1,'name' => 'abc','overall' => array('MP' => '2','W'=> '1','L' => '1','D'=> '0','PF' => '2320','PA' => '1974','PD' => '346','P' => '3')),
    array('id' => 1,'name' => 'abc','overall' => array('MP' => '2','W'=> '1','L' => '1','D'=> '0','PF' => '1620','PA' => '1824','PD' => '-204','P' => '3')),
    array('id' => 1,'name' => 'abc','overall' => array('MP' => '2','W'=> '0','L' => '2','D'=> '0','PF' => '2200','PA' => '2300','PD' => '-100','P' => '0'))
);

HTML:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => abc
            [overall] => Array
                (
                    [MP] => 2
                    [W] => 0
                    [L] => 2
                    [D] => 0
                    [PF] => 2200
                    [PA] => 2300
                    [PD] => -100
                    [P] => 0
                )

        )

    [1] => Array
        (
            [id] => 1
            [name] => abc
            [overall] => Array
                (
                    [MP] => 2
                    [W] => 1
                    [L] => 1
                    [D] => 0
                    [PF] => 2320
                    [PA] => 1974
                    [PD] => 346
                    [P] => 3
                )

        )

    [2] => Array
        (
            [id] => 1
            [name] => abc
            [overall] => Array
                (
                    [MP] => 2
                    [W] => 1
                    [L] => 1
                    [D] => 0
                    [PF] => 1620
                    [PA] => 1824
                    [PD] => -204
                    [P] => 3
                )

        )

    [3] => Array
        (
            [id] => 1
            [name] => abc
            [overall] => Array
                (
                    [MP] => 2
                    [W] => 2
                    [L] => 0
                    [D] => 0
                    [PF] => 2904
                    [PA] => 1932
                    [PD] => 972
                    [P] => 6
                )

        )

)

想要这样的。

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => abc
            [overall] => Array
                (
                    [MP] => 2
                    [W] => 2
                    [L] => 0
                    [D] => 0
                    [PF] => 2904
                    [PA] => 1932
                    [PD] => 972
                    [P] => 6
                )

        )

    [1] => Array
        (
            [id] => 1
            [name] => abc
            [overall] => Array
                (
                    [MP] => 2
                    [W] => 1
                    [L] => 1
                    [D] => 0
                    [PF] => 2320
                    [PA] => 1974
                    [PD] => 346
                    [P] => 3
                )

        )

    [2] => Array
        (
            [id] => 1
            [name] => abc
            [overall] => Array
                (
                    [MP] => 2
                    [W] => 1
                    [L] => 1
                    [D] => 0
                    [PF] => 1620
                    [PA] => 1824
                    [PD] => -204
                    [P] => 3
                )

        )

    [3] => Array
        (
            [id] => 1
            [name] => abc
            [overall] => Array
                (
                    [MP] => 2
                    [W] => 0
                    [L] => 2
                    [D] => 0
                    [PF] => 2200
                    [PA] => 2300
                    [PD] => -100
                    [P] => 0
                )

        )

)

我试过这样的事。


    $memberArray1 = usort($memberArray, function($a, $b) {
        return  $b['overall']['P'] - $a['overall']['P'];
    });
    $memberArray1 = usort($memberArray, function($a, $b) {
        return  $b['overall']['PD'] - $a['overall']['PD'];
    });

但问题是,当我按P排序然后它可以工作但是当我像PD那样排序时,它按PD排序而不是P。

1 个答案:

答案 0 :(得分:0)

您可以将uasortrsort一起用作

uasort($abc,function($a,$b){
    $c = $a['P'] - $b['P'];
    $c .= $a['PD'] - $b['PD'];
    $c .= $a['PF'] - $b['PF'];
    $c .= $a['PA'] - $b['PA'];
    return $c;
});
array_reverse($abc);
print_r($abc);

Demo