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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:18:13+00:00 2026-05-20T12:18:13+00:00

I have a multidimensional array that stores people. Array ( id93294 => array (

  • 0

I have a multidimensional array that stores people.

Array (
   id93294 => array (
       Name => "Tom Anderson",
       Birthday => "03/17/1975",
       Hometown => 'St. Louis',
       CurrentLocation => 'Mars'
   ),
   id29349 => (array (
       Name => "Tom Anderson",
       Birthday => "03/17/1975",
       Hometown => 'New York',
       CurrentLocation => 'New York'
   )
)

Kind of like that except with more info for the people, so I want to first sort by birthdays THEN sort by another attribute (if their hometown matches their current location) but once I do the second sort on the array it loses the first sort I did using the birthdays…

How do I sort multiple times without it messing up my previous sorts.

P.S. I am using uasort.

  • 1 1 Answer
  • 1 View
  • 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-20T12:18:14+00:00Added an answer on May 20, 2026 at 12:18 pm

    Update

    I recently answered this question in a much more capable manner in the “definitive” topic on sorting multidimensional arrays. You can safely skip reading the rest of this answer and directly follow the link for a much more capable solution.

    Original answer

    The function uasort lets you define your own comparison function. Simply put all the criteria you want inside that.

    For example, to sort by birthday and then by name:

    function comparer($first, $second) {
        // First see if birthdays differ
        if ($first['birthday'] < $second['birthday']) {
            return -1;
        }
        else if ($first['birthday'] > $second['birthday']) {
            return 1;
        }
    
        // OK, birthdays are equal. What else?
        if ($first['name'] < $second['name']) {
            return -1;
        }
        else if ($first['name'] > $second['name']) {
            return 1;
        }
    
        // No more sort criteria. The two elements are equal.
        return 0;
    }
    

    I am ignoring the fact that in your example, the birthdays are not in a format that can be ordered by a simple comparison using the operator <. In practice you would convert them to a trivially-comparable format first.

    Update: if you think that maintaining a bunch of these multiple-criteria comparers could get ugly real fast, you find me in agreement. But this problem can be solved as any other in computer science: just add another level of abstraction.

    I ‘ll be assuming PHP 5.3 for the next example, in order to use the convenient anon function syntax. But in principle, you could do the same with create_function.

    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);
    
                // Do the actual comparison
                if ($first[$criterion] < $second[$criterion]) {
                    return -1;
                }
                else if ($first[$criterion] > $second[$criterion]) {
                    return 1;
                }
    
            }
    
            // Nothing more to compare with, so $first == $second
            return 0;
        };
    
        return $comparer;
    }
    

    You could then do:

    uasort($myArray, make_comparer('birthday', 'name'));
    

    This example possibly tries to be too clever; in general I don’t like to use functions that do not accept their arguments by name. But in this case, the usage scenario is a very strong argument for being too clever.

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

Sidebar

Related Questions

I have a PHP class that stores a complex multidimensional array, and rather than
I have an multidimensional array that looks like this: Array ( [0] => Array
Explanation I have a multidimensional array that is iterated over to created a categorized
I have a multidimensional array that looks something like this: ourThing = array( 'id'
I have a massive multidimensional array that has been serialised by PHP. It has
I have a multidimensional array that look like this: Array [1] => Array (
I have a multidimensional array, and I need to sort that array by a
I have a multidimensional array that looks like this: Array ( [0] => Array
I have a large multidimensional array structured like the sample below. [ ['name' =>
I have a multidimensional array. I need a function that checks if a specified

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.