1

I need some help with sorting a multidimensional array in php. I need the array to be grouped into months, and then sorted by month. Here's my original array:

Array
(
    [0] => Array
        (
            [IdEvent] => 5
            [EventName] => Copy of Opening snow weekend
            [Year] => 2011
            [Month] => 08
        )

    [1] => Array
        (
            [IdEvent] => 7
            [EventName] => Copy of Copy of Opening snow weekend
            [Year] => 2011
            [Month] => 07
        )

    [2] => Array
        (
            [IdEvent] => 1
            [EventName] => Opening snow weekend
            [Year] => 2011
            [Month] => 05
        )

    [3] => Array
        (
            [IdEvent] => 6
            [EventName] => Copy of Opening snow weekend
            [Year] => 2011
            [Month] => 05
        )

)

And here's what I'm hoping to get:

Array
(

    [0] => Array (

        [0] => Array
            (
                [IdEvent] => 1
                [EventName] => Opening snow weekend
                [Year] => 2011
                [Month] => 05
            )

        [1] => Array
            (
                [IdEvent] => 6
                [EventName] => Copy of Opening snow weekend
                [Year] => 2011
                [Month] => 05
            )

    )

    [1] => Array (

        [0] => Array
            (
                [IdEvent] => 7
                [EventName] => Copy of Copy of Opening snow weekend
                [Year] => 2011
                [Month] => 07
            )

    )

    [2] => Array (

        [0] => Array
            (
                [IdEvent] => 5
                [EventName] => Copy of Opening snow weekend
                [Year] => 2011
                [Month] => 08
            )
    )

)
4
  • What is the logic of your sorting? Maybe I'm missing something but I cannot see what it is you are sorting on. Commented Jun 2, 2011 at 1:59
  • @stefgosselin: Sort it by Year / Month / Day is what appears to be the pattern. Commented Jun 2, 2011 at 2:00
  • Ok, I think this is by month .. all events for a given month in their own array? Commented Jun 2, 2011 at 2:00
  • Yep I'm trying to group them by "Month" Commented Jun 2, 2011 at 2:06

2 Answers 2

2

I would use an easily sortable key for each month-container, then just use ksort

$months = array();
foreach ($input_array AS $arr) {
  $months[$arr['Month']][] = $arr;
}
ksort($months);

if you want, you can then use array_values to turn it into a standard integer-key array

Sign up to request clarification or add additional context in comments.

3 Comments

You did not consider the year, would not work when the array has key with different years.
I'm not using 'Year' at the moment, it's only pulling the current year from the database to make the first array, should have left that out when I posted it here :)
I went for something that takes the year into account. But hey if this works for you so much the better ;o)
1

Maybe, just maybe something like this:

$sortedArray = array();

foreach ($unsortedArray as $key => $row) {
  $month[$key] = $row['month'];
  $year[$key]  = $row['year'];
}

$sortedArray =  array_multisort($year, SORT_ASC, $month, SORT_ASC, $unsortedArray);
  • Totally untried, untested, exprerimental and from the top of my head.
  • You may need SORT_NUMERIC instead of SORT_ASC, I ain't sure.

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.