Php排序多维子数组键值

时间:2017-11-14 08:13:15

标签: php arrays sorting multidimensional-array

我有一个带有多维数组的数组,就像我将大尺寸数组拆分成块大块而且我正在尝试应用asc和desc这样的排序...数组函数就像array_multisort,usort我已经尝试但它根本没有按照我预期的结果工作..

数组是

 <pre>Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [rate] => 16.8
                    [details] => Array
                        (
                            [address] => 280 Lavender Street
                            [city] => Singapore


                        )

                    [packages] => Array
                        (
                            [0] => Array
                                (

                                [avg_rate] => 16.8
                            )

                    )

            )
        [1] => Array
            (
                [rate] => 16.8
                [details] => Array
                    (
                        [address] => 280 Lavender Street
                        [city] => Singapore


                    )

                [packages] => Array
                    (
                        [0] => Array
                            (

                                [avg_rate] => 16.8
                            )

                    )

            )    
            ...
    )
[1] => Array
    (
        [0] => Array
            (
                [rate] => 16.8
                [details] => Array
                    (
                        [address] => 280 Lavender Street
                        [city] => Singapore


                    )

                [packages] => Array
                    (
                        [0] => Array
                            (

                                [avg_rate] => 16.8
                            )

                    )

            )
        [1] => Array
            (
                [rate] => 16.8
                [details] => Array
                    (
                        [address] => 280 Lavender Street
                        [city] => Singapore


                    )

                [packages] => Array
                    (
                        [0] => Array
                            (

                                [avg_rate] => 16.8
                            )

                    )

            )    
            ...
    )

和我的功能

usort($result['list'], function($a, $b) {
                    if($a['packages'][0]['avg_rate'] == $b['packages'][0]['avg_rate'])
                        return 0;

                    return $a['packages'][0]['avg_rate'] < $b['packages'][0]['avg_rate'] ? -1 : 1;
                });

任何建议

0 个答案:

没有答案