按子数组排序多维数组

时间:2014-06-04 17:19:41

标签: php arrays sorting multidimensional-array

我需要像这样对数组进行排序

     [0] => Array
            (
                [id] => 5
                [stats] => Array
                    (
                        [SessionsPlayed] => 1
                    )

            )
     [1] => Array
            (
                [id] => 88
                [stats] => Array
                    (
                        [SessionsPlayed] => 6
                    )

            )
     [2] => Array
            (
                [id] => 22
                [stats] => Array
                    (
                        [SessionsPlayed] => 9
                    )

            )

这样,SessionPlayed列就像这样按降序排列。

     [2] => Array
            (
                [id] => 22
                [stats] => Array
                    (
                        [SessionsPlayed] => 9
                    )

            )
     [1] => Array
            (
                [id] => 88
                [stats] => Array
                    (
                        [SessionsPlayed] => 6
                    )

            )
     [0] => Array
            (
                [id] => 5
                [stats] => Array
                    (
                        [SessionsPlayed] => 1
                    )

            )

我试图在线查看如何解决这个问题,但我找到的所有答案只能排序一个孩子(例如id列) 这是我看过的帖子。 https://stackoverflow.com/a/16788610/1319033 https://stackoverflow.com/a/2699110/1319033

1 个答案:

答案 0 :(得分:3)

使用PHP usort

usort($array, function($a, $b) {
    return $b['stats']['SessionsPlayed'] - $a['stats']['SessionsPlayed'];
    // $b - $a for descending order
});