1

I'm getting a multidimensional array named $user_alerts generated from the SQL query. For your reference I'm printing the array here:

Array
(
    [154472] => Array
        (
            [3829802] => Array
                (
                    [34438] => 34438
                    [34442] => 34442
                    [34429] => 34429
                    [34443] => 34443
                )

            [3830078] => Array
                (
                    [34442] => 34442
                    [34438] => 34438
                    [34443] => 34443
                    [34429] => 34429
                )

            [3829803] => Array
                (
                    [34442] => 34442
                    [34438] => 34438
                    [34429] => 34429
                    [34443] => 34443
                )

        )

    [154554] => Array
        (
            [3831407] => Array
                (
                    [34438] => 34438
                    [34429] => 34429
                    [34443] => 34443
                    [34442] => 34442
                )

            [3831408] => Array
                (
                    [34429] => 34429
                    [34443] => 34443
                    [34442] => 34442
                    [34438] => 34438
                )

        )

    [154551] => Array
        (
            [3831329] => Array
                (
                    [34442] => 34442
                    [34438] => 34438
                    [34443] => 34443
                    [34429] => 34429
                )

        )

    [154477] => Array
        (
            [3830046] => Array
                (
                    [34442] => 34442
                    [34429] => 34429
                    [34438] => 34438
                    [34443] => 34443
                )

            [3829564] => Array
                (
                    [34429] => 34429
                    [34442] => 34442
                    [34438] => 34438
                    [34443] => 34443
                )

        )

    [154474] => Array
        (
            [3829402] => Array
                (
                    [34429] => 34429
                    [34442] => 34442
                    [34438] => 34438
                    [34443] => 34443
                )

        )

    [154473] => Array
        (
            [3829377] => Array
                (
                    [34442] => 34442
                    [34429] => 34429
                    [34438] => 34438
                    [34443] => 34443
                )

        )

    [154541] => Array
        (
            [3831211] => Array
                (
                    [34442] => 34442
                    [34438] => 34438
                    [34443] => 34443
                    [34429] => 34429
                )

        )

    [154514] => Array
        (
            [3830597] => Array
                (
                    [34442] => 34442
                    [34438] => 34438
                    [34429] => 34429
                    [34443] => 34443
                )

        )

    [154513] => Array
        (
            [3830557] => Array
                (
                    [34442] => 34442
                    [34438] => 34438
                    [34429] => 34429
                    [34443] => 34443
                )

        )

    [154493] => Array
        (
            [3830072] => Array
                (
                    [34442] => 34442
                    [34429] => 34429
                    [34438] => 34438
                    [34443] => 34443
                )

            [3829611] => Array
                (
                    [34429] => 34429
                )

        )

    [154491] => Array
        (
            [3829998] => Array
                (
                    [34429] => 34429
                    [34443] => 34443
                    [34442] => 34442
                    [34438] => 34438
                )

        )

)

Now I want to sort the internal array in ascending order. The sample internal array which needs to be sorted is as follows(the first inner array element).

 [3829802] => Array
                    (
                        [34438] => 34438
                        [34442] => 34442
                        [34429] => 34429
                        [34443] => 34443
                    )

Actually I want to sort all such inner arrays. I tried lot of tricks but nothing gave me the desired result. Can anyone help me to resolve this issue? Thanks in advance.

4
  • "I tried lot of tricks" Yes? Which ones? Just looping over the arrays and sort their children is not valid solution for you? Commented Jul 10, 2013 at 11:49
  • 1
    Can you modify the query with an ORDER BY? Commented Jul 10, 2013 at 11:50
  • Sorting arrays is a trivial task, but I completely agree with @DevlshOne in the regard that instead of solving a problem, you should try and avoid making one by ordering the data in your query Commented Jul 10, 2013 at 11:51
  • Oder by does not works for me becuase array is not just coming from DB. we are inserting another array into that Commented Jul 10, 2013 at 12:03

1 Answer 1

3

When you generate the array is the best time to sort it. When you build up the arrays simply call ksort($array).

If for whatever reason that's not feasible or possible you can just loop over the arrays and sort:

foreach($array as $key => $value){
    foreach($value as $innerKey => $inner){
        ksort($inner);
        $array[$key][$innerKey] = $inner;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.