按值对多维复杂数组进行排序

时间:2015-02-06 06:34:37

标签: php sorting

我的数据格式如下:

Array
(
    [1] => User Object
        (
            [name] => 'John'
            [sAdr] => 'Addr1'
            [Children] => Array
                (
                    [101] => Children Object
                        (

                            [aChildren] => Array
                                (
                                    [0] => Child Object
                                        (
                                            [age] => 21
                                        )

                                    [1] => Child Object
                                        (

                                            [age] => 19
                                        )

                                )

                        )

                )

        )

    [2] => User Object
        (
            [name] => 'Jane'
            [sAdr] => 'Addr2'
            [Children] => Array
                (
                    [101] => Children Object
                        (

                            [aChildren] => Array
                                (
                                    [2] => Child Object
                                        (
                                            [age] => 32
                                        )

                                    [3] => Child Object
                                        (
                                            [age] => 17
                                        )

                                )

                        )

                )

        )

)

我想按年龄排序。我见过的例子并没有帮助我实现这种排序。有人可以给我一些指导。我不需要完整的逻辑,只需要了解如何实现这一目标。

预期产出:

Array
    (
    [1] => User Object
        (
            [name] => 'John'
            [sAdr] => 'Addr1'
            [Children] => Array
                (
                    [101] => Children Object
                        (

                            [aChildren] => Array
                                (
                                    [0] => Child Object
                                        (
                                            [age] => 19
                                        )

                                    [1] => Child Object
                                        (

                                            [age] => 21
                                        )

                                )

                        )

                )

        )

    [2] => User Object
        (
            [name] => 'Jane'
            [sAdr] => 'Addr2'
            [Children] => Array
                (
                    [101] => Children Object
                        (

                            [aChildren] => Array
                                (
                                    [2] => Child Object
                                        (
                                            [age] => 17
                                        )

                                    [3] => Child Object
                                        (
                                            [age] => 32
                                        )

                                )

                        )

                )

        )

)

2 个答案:

答案 0 :(得分:0)

您需要使用usort。它使用用户提供的比较函数按值对数组进行排序。

请查看http://php.net/manual/en/function.usort.php

检查它的示例,以便您了解如何实现这一目标。

答案 1 :(得分:-1)

您可以使用简单的循环来完成。

喜欢:

foreach($array as $a)
{
    //some code
}