按值排序多维数组并保存

时间:2015-09-27 11:25:03

标签: php arrays sorting

如何根据" season"的值对这个数组进行排序?首先是领域,然后是"系列"领域?并保存密钥

        Array
        (
            [2] => Array
                (
                    [0] => Array
                        (
                            [season] => 2
                            [series] => 3
                            [title] => Boom
                        )

                    [1] => Array
                        (
                            [season] => 2
                            [series] => 1
                            [title] => Boom
                        )

                    [2] => Array
                        (
                            [season] => 2
                            [series] => 2
                            [title] => Boom
                        )

                )

            [1] => Array
                (
                    [0] => Array
                        (
                            [season] => 1
                            [series] => 1
                            [title] => Boom
                        )

                    [1] => Array
                        (
                            [season] => 1
                            [series] => 3
                            [title] => Boom
                        )

                    [2] => Array
                        (
                            [season] => 1
                            [series] => 2
                            [title] => Boom
                        )

                )
        )
    )

usort,ksort函数不保存键,但按季节和系列值排序确实很好

在usort功能之后:

        Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [season] => 1
                            [series] => 1
                            [title] => Boom
                        )

                    [1] => Array
                        (
                            [season] => 1
                            [series] => 2
                            [title] => Boom
                        )

                    [2] => Array
                        (
                            [season] => 1
                            [series] => 3
                            [title] => Boom
                        )

                )

            [1] => Array
                (
                    [0] => Array
                        (
                            [season] => 2
                            [series] => 1
                            [title] => Boom
                        )

                    [1] => Array
                        (
                            [season] => 2
                            [series] => 2
                            [title] => Boom
                        )

                    [2] => Array
                        (
                            [season] => 2
                            [series] => 3
                            [title] => Boom
                        )

                )
        )

谁能帮我解决这个问题?

1 个答案:

答案 0 :(得分:0)

由array_walk解决

array_walk($arr, function($a, $b){
        return $a['season'] - $b['season'];
    });