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

  • Home
  • SEARCH
  • 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 6577727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:40:31+00:00 2026-05-25T15:40:31+00:00

Possible Duplicate: Sorting multidimensional array in PHP How can I sort by key in

  • 0

Possible Duplicate:
Sorting multidimensional array in PHP

How can I sort by key in a multidimensional array?

For instance, below is the array I print from my db, where the latest comes first – December, November, October, etc and 2011, 2010, 2009, etc

Array
(
    [0] => Array
        (
            [URL] => september 2011
            [Title] => September 2011
            [Date] => 8
            [Month] => 9
            [Year] => 2011
        )

    [1] => Array
        (
            [URL] => january 2011
            [Title] => January 2011
            [Date] => 1
            [Month] => 2
            [Year] => 2011
        )

    [2] => Array
        (
            [URL] => february 2011
            [Title] => February 2011
            [Date] => 4
            [Month] => 1
            [Year] => 2011
        )

    [3] => Array
        (
            [URL] => november 2011
            [Title] => November 2011
            [Date] => 23
            [Month] => 11
            [Year] => 2010
        )

    [4] => Array
        (
            [URL] => april 2011
            [Title] => April 2011
            [Date] => 23
            [Month] => 4
            [Year] => 2010
        )

)

But I need it to be like this, October, November, December, etc and 2011, 2010, 2009, etc – note the months are sorted by the oldest comes first but the years are still sorted by the latest comes first.

So the array should be sorted like this,

Array
(

    [2] => Array
        (
            [URL] => february 2011
            [Title] => February 2011
            [Date] => 4
            [Month] => 1
            [Year] => 2011
        )

    [1] => Array
        (
            [URL] => january 2011
            [Title] => January 2011
            [Date] => 1
            [Month] => 2
            [Year] => 2011
        )

    [0] => Array
        (
            [URL] => september 2011
            [Title] => September 2011
            [Date] => 8
            [Month] => 9
            [Year] => 2011
        )

    [4] => Array
        (
            [URL] => april 2010
            [Title] => April 2010
            [Date] => 23
            [Month] => 4
            [Year] => 2010
        )

    [3] => Array
        (
            [URL] => november 2010
            [Title] => November 2010
            [Date] => 23
            [Month] => 11
            [Year] => 2010
        )
)

Is that possible?

  • 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-05-25T15:40:32+00:00Added an answer on May 25, 2026 at 3:40 pm

    Generic solution to sort arrays of arrays with multiple keys

    Based on my answer to this question, here is a very generic solution that you can use in lots of situations.

    Limitation: Requires PHP >= 5.3 to work, due to the presence of anonymous functions.

    New and improved, now with descending sort support

    function make_comparer() {
        $criteriaNames = func_get_args();
        $comparer = function($first, $second) use ($criteriaNames) {
            // Do we have anything to compare?
            while(!empty($criteriaNames)) {
                // What will we compare now?
                $criterion = array_shift($criteriaNames);
    
                // Used to reverse the sort order by multiplying
                // 1 = ascending, -1 = descending
                $sortOrder = 1; 
                if (is_array($criterion)) {
                    $sortOrder = $criterion[1] == SORT_DESC ? -1 : 1;
                    $criterion = $criterion[0];
                }
    
                // Do the actual comparison
                if ($first[$criterion] < $second[$criterion]) {
                    return -1 * $sortOrder;
                }
                else if ($first[$criterion] > $second[$criterion]) {
                    return 1 * $sortOrder;
                }
    
            }
    
            // Nothing more to compare with, so $first == $second
            return 0;
        };
    
        return $comparer;
    }
    

    How to use it

    To sort by year ascending:

    uasort($array, make_comparer('Year'));
    

    To sort by year ascending, then by month ascending:

    uasort($array, make_comparer('Year', 'Month'));
    

    To sort by year descending, then by month ascending:

    uasort($array, make_comparer(array('Year', SORT_DESC), 'Month'));
    

    This last one is what you ‘re after.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Sorting a multidimensional array in PHP? $songs = array( '1' => array('artist'=>'The
Possible Duplicate: Which sorting algorithm is used by STL’s list::sort()? Which sorting algorithm can
Possible Duplicate: Sorting an associative array in PHP Hello all, I have an array
Possible Duplicate: Sorting a multidimensional array? I have an a multi dimesional associative array:
Possible Duplicate: Sorting an associative array in PHP I have an array that looks
Possible Duplicate: C++ struct sorting Is it possible to sort a vector in C++
Possible Duplicate: Python not sorting unicode properly. Strcoll doesn't help. I'm trying to sort
Possible Duplicate: Find out the instance id from within an ec2 machine I am
Possible Duplicate: Sorting a hash in Ruby by its value first then its key.
Possible Duplicate: sorting in nsmutable array I have 2 NSMutableArray array. I have to

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.