简体   繁体   中英

Sort Multi-dimensional Array by Value and save

How can I sort this array by the value of the "seasons" field first, then of the "series" field? And save the keys

        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 functions does not save the keys, but sorting by seasons and series values really good

After usort function:

        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
                        )

                )
        )

Who can help me solve this problem?

Solved by array_walk

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM