-2

I want to sort array by value P if two P are same then sort by PD if two PD is same then sort by PF same as PA.
How i achieve this?

I have Multidimensional array In PHP Like

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
                )

        )

)

Want similar like this.

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
                )

        )

)

I have tried like this.


    $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'];
    });

But the issue is when i sort like P then it works but after when i sort like PD then it sort by PD not like P.

Ishan Shah
  • 1,665
  • 2
  • 20
  • 42

1 Answers1

0

You can use uasort along with rsort as

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

Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54