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 3241712
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:13:51+00:00 2026-05-17T18:13:51+00:00

How do you sort a multidimensional array by primary and secondary key? For example,

  • 0

How do you sort a multidimensional array by primary and secondary key?
For example, assuming the following array:

$result = array();

$result[0]["prio"] = 1;
$result[0]["date"] = '2010-02-28';
$result[0]["post"] = "February's thoughts";

$result[1]["prio"] = 0;
$result[1]["date"] = '2010-04-20';
$result[1]["post"] = "April's thoughts";

$result[2]["prio"] = 0;
$result[2]["date"] = '2010-05-30';
$result[2]["post"] = "May's thoughts";

I wish to sort the column ‘prio’ as the primary key (ascending) and ‘date’ as the secondary key (descending), to obtain:

$result[0]["prio"] = 0;
$result[0]["date"] = '2010-05-30';
$result[0]["post"] = "May's thoughts";
$result[1]["prio"] = 0;
$result[1]["date"] = '2010-04-20';
$result[1]["post"] = "April's thoughts";
$result[2]["prio"] = 1;
$result[2]["date"] = '2010-02-28';
$result[2]["post"] = "February's thoughts";

How to achieve this?

  • 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-17T18:13:51+00:00Added an answer on May 17, 2026 at 6:13 pm

    Use usort like this:

    $result = array();
    
    $result[0]["prio"] = 1;
    $result[0]["date"] = '2010-02-28';
    $result[0]["post"] = "February's thoughts";
    
    $result[1]["prio"] = 0;
    $result[1]["date"] = '2010-04-20';
    $result[1]["post"] = "April's thoughts";
    
    $result[2]["prio"] = 0;
    $result[2]["date"] = '2010-05-30';
    $result[2]["post"] = "May's thoughts";
    
    function fct($a ,$b) {
    
      if ($a['prio'] < $b['prio']) {
        return -1;
      } elseif  ($a['prio'] > $b['prio']) {
        return 1;
      } else {
        return strcmp($b['date'], $a['date']);
      }
    
    }
    
    usort($result, "fct");
    print_r($result);
    

    Output:

    Array
    (
        [0] => Array
            (
                [prio] => 0
                [date] => 2010-05-30
                [post] => May's thoughts
            )
    
        [1] => Array
            (
                [prio] => 0
                [date] => 2010-04-20
                [post] => April's thoughts
            )
    
        [2] => Array
            (
                [prio] => 1
                [date] => 2010-02-28
                [post] => February's thoughts
            )
    
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: PHP Sort a multidimensional array by element containing date I have some
Possible Duplicate: Sorting multidimensional array in PHP How can I sort by key in
I'm trying to sort a multidimensional array in objective-c i know that i can
Possible Duplicate: php sort array by sub-value I have a multidimensional array like the
I have a very large multidimensional array and I would like to sort it
Possible Duplicate: how to sort a multidemensional array by an inner key How to
Possible Duplicate: Sort multidimensional array by multiple keys I am trying to find some
I have a multidimensional array. The primary array is an array of [publicationID][publication_name][ownderID][owner_name] What
How would you sort a multidimensional array in JavaScript? I have an array full
I'm trying to sort a multidimensional array by multiple keys, and I have no

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.