Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8396757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:37:13+00:00 2026-06-09T20:37:13+00:00

I know this question has been asked several times but the answers given don’t

  • 0

I know this question has been asked several times but the answers given don’t help my situation.

I have a simple array that basically represent the month and year. The only purpose of the array is to keep track of events in a database and I need to know which months and years are represented. The array is basically:

$array[MONTH][YEAR] = 1; //just any value. Don't care about the value.

I need to sort the array so that the years are in order but also any months in the same year should also be sorted. See the output I want below…

$dates[10][2012] = 1;
$dates[1][2011] = 1;
$dates[12][2013] = 1;
$dates[4][2010] = 1;
$dates[6][2009] = 1;
$dates[7][2009] = 1;

How do I sort this array so that the values come back as:

Array
(
    [6] => Array
    (
        [2009] => 1
    )
    [7] => Array
    (
        [2009] => 1
    )
    [4] => Array
    (
        [2010] => 1
    )
    [1] => Array
    (
        [2011] => 1
    )
    [10] => Array
    (
        [2012] => 1
    )
    [12] => Array
    (
        [2013] => 1
    )
}

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:37:15+00:00Added an answer on June 9, 2026 at 8:37 pm

    Try using uasort. You can write a custom function to sort on the inner keys rather than the outer ones.

    EDIT:

    It turns out that you need to use uksort if you want to sort the outer months as well. This seems to work:

    uksort($dates, function ($a, $b) use ($dates) {
      $year = key($dates[$a]) - key($dates[$b]);
      $month = $a - $b;
      return $year == 0 ? $month : $year;
    });
    

    EDIT:

    If you just change the index order like Jon suggested above it’s a lot simpler:

    $dates[2012][10] = 1;
    $dates[2011][1] = 1;
    $dates[2013][12] = 1;
    $dates[2010][4] = 1;
    $dates[2009][7] = 1;
    $dates[2009][6] = 1;
    
    ksort($dates);
    array_walk($dates, ksort);
    
    print_r($dates);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this question has been asked several times, but I can't quite seem
I know this question has been asked several times on SO, but none of
I know this question has been asked several times for sure, but I have
I know this question has been asked elsewhere but reading the already given answers
I know this question has been asked several times, but I would like to
I know that this question has been asked several times and I've read all
I know this question has been asked a zillion times, but I still feel
I know this question has been asked before numorous times but they seem to
I know this question has been asked countless of times. But none of them
I know that this question have been asked several times. But I can't get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.