按两个值排序多维数组

时间:2017-09-02 14:00:32

标签: php arrays sorting

我有这个数组

Array
(
    [arr1] => Array
        (
            [0] => Array
                (                   
                    [m] => name1
                    [p] => 261.15
                    [f] => 0.30112588
                )

            [1] => Array
                (                
                    [m] => name2
                    [p] => 214.92
                    [f] => 0.35793662
                )

            [2] => Array
                (
                    [m] => name3
                    [p] => 504.35
                    [f] => 0.35779595
                )

            [3] => Array
                (
                    [m] => name3
                    [p] => 504.35
                    [f] => 0.31910875
                )

            [4] => Array
               (
                    [m] => name3
                    [p] => 504.35
                    [f] => 0.38972548
                )

            [5] => Array
                (
                    [m] => name4
                    [p] => 5500
                    [f] => 0.50674623
                )

            [6] => Array
                (
                    [m] => name5
                    [p] => 600.5
                    [f] => 0.11483321
                )
        )
)

我想对这个数组进行排序,所以带有最高['p']的项目位于顶部,但我也想要一些项目具有相同的['m']和相同的['p']而不是具有最低['p']的项目f']位于这些之上,具有相同的['m']和['p']。

我设法通过['p']使用usort函数对这个数组进行排序,但是只有当它们具有相同的['m']和['p']时才能按['f']排序。

代码:

$arr = array();
$arr['arr1'] = array();

array_push($arr['arr1'], ['m' => 'name1', 'p' => 261.15 ,'f' => 0.30112588]);
array_push($arr['arr1'], ['m' => 'name2', 'p' => 214.92 ,'f' => 0.35793662]);
array_push($arr['arr1'], ['m' => 'name3', 'p' => 504.35 ,'f' => 0.35779595]);
array_push($arr['arr1'], ['m' => 'name3', 'p' => 504.35 ,'f' => 0.31910875]);
array_push($arr['arr1'], ['m' => 'name3', 'p' => 504.35 ,'f' => 0.38972548]);
array_push($arr['arr1'], ['m' => 'name4', 'p' => 5500 ,'f' => 0.50674623]);
array_push($arr['arr1'], ['m' => 'name5', 'p' => 600.5 ,'f' => 0.11483321]);

1 个答案:

答案 0 :(得分:1)

使用usort功能:

条件清单:

  • 对此数组进行排序,因此最高['p']的项目位于顶部

  • ['f'] ['m']

  • 项目中['p']项最低的项目在顶部上。

// $arr is your array
usort($arr, function($a,$b){
    if ($a['m'] == $b['m'] && $a['p'] == $b['p']) {
        return ($a['f'] > $b['f'])? 1 : -1;
    }
    return ($b['p'] > $a['p'])? 1 : -1;
});

print_r($arr);

输出:

Array
(
    [0] => Array
        (
            [m] => name4
            [p] => 5500
            [f] => 0.50674623
        )

    [1] => Array
        (
            [m] => name3
            [p] => 504.35
            [f] => 0.31910875
        )

    [2] => Array
        (
            [m] => name3
            [p] => 504.35
            [f] => 0.35779595
        )

    [3] => Array
        (
            [m] => name3
            [p] => 504.35
            [f] => 0.38972548
        )

    [4] => Array
        (
            [m] => name5
            [p] => 500.5
            [f] => 0.11483321
        )

    [5] => Array
        (
            [m] => name1
            [p] => 261.15
            [f] => 0.30112588
        )

    [6] => Array
        (
            [m] => name2
            [p] => 214.92
            [f] => 0.35793662
        )
)