Home > Net >  Sort multidimensional array with array_multisort
Sort multidimensional array with array_multisort

Time:12-06

I have an array:

$arr = [
            [
                "t1" => ["sum" => 3],
                "t2" => ["sum" => 8],
                "t3" => ["sum" => 3],
                "t4" => ["sum" => 1],
                "t5" => ["sum" => 8]
            ],
            [
                "t1" => ["sum" => 6],
                "t2" => ["sum" => 2],
                "t3" => ["sum" => 4],
                "t4" => ["sum" => 4],
                "t5" => ["sum" => 1]
            ]
        ];

How to sort this array using array_multisort to get an array like this?

Array
(
    [0] => Array
        (
            [t2] => 8
            [t5] => 8
            [t1] => 3
            [t3] => 3
            [t4] => 1
        )

    [1] => Array
        (
            [t2] => 2
            [t5] => 1
            [t1] => 6
            [t3] => 4
            [t4] => 4
        )

)

First we sort by the first sub array (index = 0), then by the second sub array (index = 1).

My code:

$arr = [
            [
                "t1" => ["sum" => 3],
                "t2" => ["sum" => 8],
                "t3" => ["sum" => 3],
                "t4" => ["sum" => 1],
                "t5" => ["sum" => 8]
            ],
            [
                "t1" => ["sum" => 6],
                "t2" => ["sum" => 2],
                "t3" => ["sum" => 4],
                "t4" => ["sum" => 4],
                "t5" => ["sum" => 1]
            ]
        ];
        
array_multisort(array_column($arr[0], "sum"), SORT_DESC, SORT_NUMERIC,
                array_column($arr[1], "sum"), SORT_NUMERIC, SORT_DESC);

print_r($arr);

But the result is wrong:

Array
(
    [0] => Array
        (
            [t1] => Array
                (
                    [sum] => 3
                )

            [t2] => Array
                (
                    [sum] => 8
                )

            [t3] => Array
                (
                    [sum] => 3
                )

            [t4] => Array
                (
                    [sum] => 1
                )

            [t5] => Array
                (
                    [sum] => 8
                )

        )

    [1] => Array
        (
            [t1] => Array
                (
                    [sum] => 6
                )

            [t2] => Array
                (
                    [sum] => 2
                )

            [t3] => Array
                (
                    [sum] => 4
                )

            [t4] => Array
                (
                    [sum] => 4
                )

            [t5] => Array
                (
                    [sum] => 1
                )

        )

)

CodePudding user response:

array_column function returns a new array of values representing a single column from the input array and you are passing two arrays created with this function to the array_multisort which applies the changes to the arrays passed as params but you don't have pointers to those 2 arrays that have been sorted. That is to say that the array_multisort function is sorting array_column(...) and (array_column(...) not the $arr. The following can solve your problem:

$arr = [
            [
                "t1" => ["sum" => 3],
                "t2" => ["sum" => 8],
                "t3" => ["sum" => 3],
                "t4" => ["sum" => 1],
                "t5" => ["sum" => 8]
            ],
            [
                "t1" => ["sum" => 6],
                "t2" => ["sum" => 2],
                "t3" => ["sum" => 4],
                "t4" => ["sum" => 4],
                "t5" => ["sum" => 1]
            ]
        ];
$arr0 = array_column($arr[0], "sum");
$arr1 = array_column($arr[1], "sum");
        
array_multisort($arr0, SORT_DESC, SORT_NUMERIC,
                $arr1, SORT_NUMERIC, SORT_DESC);

print_r([$arr0, $arr1]);
  •  Tags:  
  • php
  • Related