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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:18:19+00:00 2026-06-04T00:18:19+00:00

I have the following array that I need to have sorted from highest score

  • 0

I have the following array that I need to have sorted from highest score to lowest.

Array
(
    [0] => Array
        (
            [team id] => 5
            [score] => 52
            [fouls] => 0
        )

    [1] => Array
        (
            [team id] => 4
            [score] => 47
            [fouls] => 0
        )

    [2] => Array
        (
            [team id] => 8
            [score] => 46
            [fouls] => 6
        )

    [3] => Array
        (
            [team id] => 1
            [score] => 46
            [fouls] => 5
        )

    [4] => Array
        (
            [team id] => 9
            [score] => 46
            [fouls] => 3
        )

)

The array above has been sorted already with this function:

function sortByOrder($a, $b){
    $Return = $b['score'] - $a['score'];
    return $Return;
}

usort($Array, 'sortByOrder');

Which seems to work fine since the array with the highest score goes up the list.

However, some teams have equal scores but have committed some fouls. How can I adjust the function to also take the fouls into account when a score equals another score? The team with the least fouls should be placed higher.

Looking forward for creative solutions!

Thanks in advance.

  • 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-04T00:18:21+00:00Added an answer on June 4, 2026 at 12:18 am

    You just have to update your function sortByOrder().

    Basically:

    function sortByOrder($a, $b){
      if ($b['score'] == $a['score'])
        return $a['fouls'] - $b['fouls'];
      else
        return $b['score'] - $a['score'];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following array, that I need to operate by hand on bitmaps. const
I have the following php code that gets a serialized array from a mysql
I have the following array that I need to recursively loop through and remove
Lets say that i have the following array : float QuadVertices[4 * 2]; float
Lets say I have an array numbers that contains the following values: int numbers
I have written a KornShell (ksh) script that sets an array the following way:
I have an array that looks like the following: [ { type: 'A', price:
I have an array of entities that need to be replaced in a large
I have an array of products that I'm pulling from the db. I've verified
I have a 3D array. I need to extract the following: the number of

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.