-1

I have data in the following format:

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
                                        )

                                )

                        )

                )

        )

)

I want to sort it by age. The examples that i have seen didn't help me to achieve the sort. Can someone give me some direction. I dont need full logic, just some clues on how to achieve this.

Expected Output:

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
                                        )

                                )

                        )

                )

        )

)
  • Sort what by age exactly? The first array level sorted by the *ages* of their children? How exactly should that work? Or the children in each array element? What do you expect the result to be? – deceze Feb 06 '15 at 06:37
  • Take a look at http://stackoverflow.com/questions/1597736/how-to-sort-an-array-of-associative-arrays-by-value-of-a-given-key-in-php?rq=1 – MisterBla Feb 06 '15 at 06:37
  • I have edited my question and written the expected output. – TinTin1 Feb 06 '15 at 06:39
  • So, is there always going to be exactly one `['Children'][101]['aChildren']` array? Or multiple? Or what? – deceze Feb 06 '15 at 07:15
  • It can be multiple. I just managed to do that myself. Sorry for taking your time. – TinTin1 Feb 06 '15 at 08:06

2 Answers2

0

You need to use usort. It sorts an array by its values using a user-supplied comparison function.

Please take a look at http://php.net/manual/en/function.usort.php

Check examples of it so that you'll get idea how to achieve this.

J.K.A.
  • 7,272
  • 25
  • 94
  • 163
-1

You can do it by using a simple loop.

Like:

foreach($array as $a)
{
    //some code
}
Wolfack
  • 2,667
  • 1
  • 26
  • 50