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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:19:38+00:00 2026-06-05T02:19:38+00:00

I have a table/array. E.g. in Excel I can sort by column 1 asc,

  • 0

I have a table/array. E.g. in Excel I can sort by column 1 asc, column 2 desc, column 3 asc, etc.

Can I do same in PHP? First with ["first_name"] ASC, then ["last_name"] DESC, ["player_id"] ASC and ["user_id"] DESC.

array(2) {
    [0]=> array(6) {
        [0]=> string(8) "John",
        ["first_name"]=> string(8) "John",
        [1]=> int(7) "44",
        ["score"]=> int(7) "44",
        [2]=> string(2) "7",
        ["player_id"]=> string(2) "7",
        [3]=> string(2) "3",
        ["user_id"]=> string(2) "3"
    },
    [1]=> array(6) {
        [0]=> string(5) "Sam",
        ["first_name"]=> string(5) "Sam",
        [1]=> int(7) "55",
        ["score"]=> int(7) "55",
        [2]=> string(2) "1",
        ["player_id"]=> string(2) "1",
        [3]=> string(2) "6",
        ["user_id"]=> string(2) "61"
    }
}

(The array is much longer and deeper in reality, this is just an example.)

Update:

function byPlayerID($player, $compare) {
    if($player['player_id'] > $compare['player_id'])
        return 1; // move up
    else if($player['player_id'] < $compare['player_id'])
        return -1; // move down
    else
        return 0; // do nothing

    if($player['score'] > $compare['score'])
        return 1; // move up
    else if($player['score'] < $compare['score'])
        return -1; // move down
    else
        return 0; // do nothing
}

Update 2: Never mind, I just needed to remove return 0;

  • 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-05T02:19:39+00:00Added an answer on June 5, 2026 at 2:19 am

    Use usort().

    Example:

    $byPlayerID = function($player, $compare) {
      if($player['player_id'] > $compare['player_id'])
        return 1; // move up
      else if($player['player_id'] < $compare['player_id'])
        return -1; // move down
      else
        return 0; // do nothing
    };
    
    usort($players, $byPlayerID);
    // now $players is sorted!
    

    This does require PHP 5.3 though, below is a more backwards compatible version

    function byPlayerID($player, $compare) {
      if($player['player_id'] > $compare['player_id'])
        return 1; // move up
      else if($player['player_id'] < $compare['player_id'])
        return -1; // move down
      else
        return 0; // do nothing
    }
    
    usort($players, "byPlayerID");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have a table column in Oracle 10 which contain a string array
i have a three dimensional bit table array as bit_table[dim1][100][200]; The second and third
I have fetched the contents of a table in array of object. And the
While working with ActiveRecord I have a table which stores a serialized array of
Say I have an array of table DDL queries and I want to get
I have written a function to print database table to an array like this
I have a 2 dimensional array forming a table: [color][number][shape ] ------------------------- [black][10 ][square
Suppose I have an array that mimics a database table. Each array element represents
I have this JavaScript function to create a table with image cells: function Draw(array)
I want to uapdate a table field with an array.That means i have a

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.