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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:51:07+00:00 2026-06-16T08:51:07+00:00

Possible Duplicate: How do I sort a multidimensional array in php …….. This is

  • 0

Possible Duplicate:
How do I sort a multidimensional array in php

……..

This is not a duplicate. There are indeed tons of questions on stackoverflow regarding sorting multi-dimensional arrays (I believe I read most of them yesterday, before posting my question…), but none that I have seen refer to my particular problem: sorting by a varriying number of fields. The answer given by @dev-null-dweller below does just this.

……..

I’m trying to create a flexible sort function for a multi-dimensional array in PHP.

The flexibility I’m after is for the number of sorting fields (could be 1, 2, or 3 for a 1st, 2nd or 3rd level sort), and for the identity of those fields.

This is a sample bit of my array:

Array
(
[0] => Array
    (
        [title] => Australia Book
        [price] => 209.00
        [code] => 3571313
        [con] => Australia & Oceania
        [ctry] => Australia
    )

[1] => Array
    (
        [title] => New Zealand Book
        [price] => 169.00
        [code] => 3571346
        [con] => Australia & Oceania
        [ctry] => New Zealand
    )

[2] => Array
    (
        [title] => Austria Book
        [price] => 129.00
        [code] => 3571230
        [con] => Europe
        [ctry] => Austria
    )

[3] => Array
    (
        [title] => Austria Pocket Book
        [price] => 119.00
        [code] => 3571354
        [con] => Europe
        [ctry] => Austria
    )



[4] => Array
    (
        [title] => Amsterdam Book
        [price] => 59.00
        [code] => 3571350
        [con] => Europe
        [ctry] => Netherlands
    )

[5] => Array
    (
        [title] => Britain
        [price] => 59.00
        [code] => 3571315
        [con] => Europe
        [ctry] => Britain
    )
)

I sometimes need to sort only by title, sometimes by ctry then title, sometimes by con, then ctry then title, etc.

I’ve managed to make a 2 level sort like so:

usort($arrCatalog, array('Catalogs_model', 'multi_compare'));

function multi_compare($a ,$b) {
if ($a['con'] < $b['con']) {
    return -1;
  } elseif  ($a['con'] > $b['con']) {
    return 1;
  } else {
    return strcmp($a['title'], $b['title']);
  }

}

The first question is how can I pass parameters to multi_compare function, when called from within usort.

After we settle that, I would be happy for some pointers as to how can I alter multi_sort so the identity and\or the number of fields will vary.

  • 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-16T08:51:09+00:00Added an answer on June 16, 2026 at 8:51 am

    You are using static method for custom sorting, so you can set some static properties in this class before usort

    class Catalogs_model{
        public static $usort_criteria = array();
    
        public static function multi_compare($a,$b){
            foreach(self::$usort_criteria as $what => $order){
                if($a[$what] == $b[$what]){
                    continue;
                }
                return (($order == 'desc')?-1:1) * strcmp($a[$what], $b[$what]);
            }
            return 0;
        }
    }
    
    Catalogs_model::$usort_criteria = array(
       'con' => 'asc',
       'title' => 'asc'
    );
    usort($arrCatalog, array('Catalogs_model', 'multi_compare'));
    

    of course it needs some tweaking , because now it only sorts strings.

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

Sidebar

Related Questions

Possible Duplicate: php sort array by sub-value I have a multidimensional array like the
Possible Duplicate: How do I sort a multidimensional array in php I have value
Possible Duplicate: How do I sort a multidimensional array in php i need to
Possible Duplicate: Sort array of objects sort an object array in php EDIT: This
Possible Duplicate: PHP Sort a multidimensional array by element containing date I have some
Possible Duplicate: How do I sort a multidimensional array in php Sorting an associative
Possible Duplicate: How do I sort a multidimensional array in php Array ( [0]
Possible Duplicate: How do I sort a multidimensional array in php I have multidimensional
Possible Duplicate: Sorting an associative array in PHP How to sort a multidimensional array
Possible Duplicate: Sorting multidimensional array in PHP How can I sort by key in

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.