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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:08:03+00:00 2026-05-23T12:08:03+00:00

EDIT : For anyone who might come across this post with a similar problem,

  • 0

EDIT: For anyone who might come across this post with a similar problem, It was solved by taking konforce‘s supplied answer and tweaking around a bit with the custom sorting function:

function cmp($a, $b) {
    if ($a[5] == $b[5]) {
        return ($a[3] < $b[3]) ? -1 :1;
    }
    return ($a[5] > $b[5]) ? -1 : 1;
}

Notice $a[5] == $b[5] does not return zero. It was changed to check who has the most losses and then sort it in ASC order. I’m sure you can even keep going and add another if-statement in there in-case they have the same losses.

Lastly, all you do is usort($ARRAY, "cmp"); and finito!!!

Original Post

My apologies for coming up with yet another MD Array sorting question but I’m just not getting it. I’ve searched aplenty
for a solution and although many sites have provided what seemed like a logical answer I still have not been able to figure it out.

My problem is since I’m still learning its been rather difficult for me to grasp the concept of using usort with a custom comparing
function. Atleast, thats what I have seen the most when others have tried to sort MD Arrays.

I’m working on a small project to sharpen up on my php skills. Its a very basic tournament standings script that holds a team’s information within an array. I would like to sort the array by most points($array[X][X][5]).

So the array looks something like this:

Array (
        [0] => Array (
            [0] => Array (
                [0] => cooller
                [1] => 6 
                [2] => 6 
                [3] => 0 
                [4] => 0 
                [5] => 18 
            )
        ) 
        [1] => Array (
            [0] => Array (
                [0] => strenx 
                [1] => 9 
                [2] => 5 
                [3] => 1 
                [4] => 3 
                [5] => 18
            )
        )
        [2] => Array (
            [0] => Array (
                [0] => rapha
                [1] => 10
                [2] => 8
                [3] => 1
                [4] => 1
                [5] => 25
            )
        ) [3] => Array (
            [0] => Array (
                [0] => ronald reagan
                [1] => 5 
                [2] => 4 
                [3] => 0 
                [4] => 1 
                [5] => 13
            )
        )
    )

I would like to sort it by most points(cell #5), so it would look like this after sorting:

Array (
        [0] => Array (
            [0] => Array (
                [0] => rapha
                [1] => 10
                [2] => 8
                [3] => 1
                [4] => 1
                [5] => 25
            )
        )
        [1] => Array (
            [0] => Array (
                [0] => cooller
                [1] => 6 
                [2] => 6 
                [3] => 0 
                [4] => 0 
                [5] => 18 
            )
        ) 
        [2] => Array (
            [0] => Array (
                [0] => strenx 
                [1] => 9 
                [2] => 5 
                [3] => 1 
                [4] => 3 
                [5] => 18
            )
        )
        [3] => Array (
            [0] => Array (
                [0] => ronald reagan
                [1] => 5 
                [2] => 4 
                [3] => 0 
                [4] => 1 
                [5] => 13
            )
        )
    )

The player with 25 points would be at the top, followed by 18, 18, and lastly 13. Sorry for my earlier post, was having difficulty wording my question correctly. Thanks in advanced!

  • 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-23T12:08:03+00:00Added an answer on May 23, 2026 at 12:08 pm

    I think you want something like this:

    usort($standings, function($a, $b) { return $b[0][5] - $a[0][5]; });
    

    Or prior to PHP 5.3:

    function cmp($a, $b) { return $b[0][5] - $a[0][5]; }
    usort($standings, 'cmp');
    

    When using usort, the $a and $b parameters will be one “layer” into the supplied array. So in your case, an example of $a or $b will be:

    [0] => Array (
             [0] => cooller
             [1] => 6 
             [2] => 6 
             [3] => 0 
             [4] => 0 
             [5] => 18 
    )
    

    I’m not sure why you have an extra containing array there, but as you can see, you want to sort based on the [0][5] position.

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

Sidebar

Related Questions

edit #2: Question solved halfways. Look below As a follow-up question, does anyone know
Does anyone know why there is no respond_to block for generated edit actions? Every
Question: Is anyone out there familiar enough with Komodo Edit to shed some light
Edit: This question was written in 2008, which was like 3 internet ages ago.
Edit: From another question I provided an answer that has links to a lot
EDIT: This was formerly more explicitly titled: - Best solution to stop Kontiki's KHOST.EXE
EDIT: This question is more about language engineering than C++ itself. I used C++
Edit : Solved, there was a trigger with a loop on the table (read
edit: in case anyone is wondering, the actionhandler invokes code that creates and disposes
EDIT: Learned that Webmethods actually uses NLST, not LIST, if that matters Our business

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.