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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:40:33+00:00 2026-05-23T17:40:33+00:00

I have a $result variable in PHP from a MySQL query & I would

  • 0

I have a $result variable in PHP from a MySQL query & I would like to sort it (with PHP) on 2 different columns.

I am doing this anytime a user clicks a different sort method, so I don’t want to have to redo the MySQL query with ORDER BY …., …. each time.

  • 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-23T17:40:33+00:00Added an answer on May 23, 2026 at 5:40 pm

    It would probably be more efficient to have MySQL do the ordering for you, but if you want to do it in PHP, check out usort which will let you sort an array using a callback function to decide the ordering.

    If your $result is an array of associative database rows, here’s a very simple example of using usort to sort on a string column ‘foo’ …

    //sort on string column
    function sortStringFoo($a, $b)
    {
        return strcmp($a['foo'], $b['foo']);
    }
    usort($result, 'sortStringFoo');
    

    Here’s how you’d do a numeric sort…

    //sort on numeric column
    function sortNumericBar($a, $b)
    {
        if ($a['bar'] == $b['bar']) {
            return 0;
        }
        return ($a['bar'] < $b['bar']) ? -1 : 1;
    }
    usort($result, 'sortNumericBar');
    

    That illustrates the basics of how to use usort, but how might we sort on several columns at once? Here’s how we could combine our comparison callbacks to illustrate this by sorting on ‘foo’ and then ‘bar’…

    function sortFooBar($a,$b)
    {
        $order=sortStringFoo($a,$b);
        if ($order==0)
           $order=sortNumericBar($a,$b);
    
        return $order;
    }
    usort($result, 'sortFooBar');
    

    You could unroll sortFooBar into a single function, but this illustrates the technique: compare first column, if same, compare second column, and so on.

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

Sidebar

Related Questions

I have a result from an SQL query that I would like to sort
I need to grab the result of a query, with php from a mysql
I have a search result from MySQL query or Array or something else. The
I have a PHP server script that SELECTs some data from a MySQL database.
I have a basic MySQL query: $getFeed = SELECT posts.postID, posts.postTitle, ..... FROM posts
I have a variable called $result which should store all the results from my
I have 2 php pages: query.php and result.php . In query.php , I am
I have a PHP function that executes a MySQL query with a few variables
I have no actual php errors, but When I make my query this I
I have a JSON result that contains numerous records. I'd like to show the

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.