[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

sorting a mutli-dimensional array in PHP

Posted on 2011-02-16
2
Medium Priority
?
220 Views
Last Modified: 2012-05-11
Hi there,

Well, the saga which is my first web application continues.  I recently had some help with an algorithm which calculates wins using a monthly tennis house-league matrix of scores and it works great.  But, with my limited exposure to web programming I've again reached a bit of a snag.

I have a 'Top Guns' sidebar.  I would like it to display the top 3 players in every house.  The information includes Name, Total Points, and now, Wins.  Here's what I'm looking to do...I would like the players to be sorted by Wins, and then by Total Points.  For example, if two players have 2 wins each..the one with the most points total points would appear ahead of the other guy.  Here's how my multi-dimensional array looks :

Array
(
    [0] => Array
        (
            [name] => Player 1
            [wins] => 3
            [score] => 24
        )

    [1] => Array
        (
            [name] => Player 2
            [wins] => 0
            [score] => 13
        )

    [2] => Array
        (
            [name] => Player 3
            [wins] => 1
            [score] => 12
        )

    [3] => Array
        (
            [name] => Player 4
            [wins] => 2
            [score] => 18
        )

)

Open in new window


I have the code to populate the array all set, I just need to be able to sort it according to my specifications.  Any help would be greatly appreciated!
0
Comment
Question by:duder78
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 15

Accepted Solution

by:
ncoo earned 2000 total points
ID: 34910045
Just pass your array in to: usort($array, "winScore");

Details: http://uk3.php.net/usort

See code below.
<?php

$array = array(0 => array
        (
            'name' => 'Player 1',
            'wins' => '3',
            'score' => '24'
        ),

    1 => array
        (
            'name' => 'Player 2',
            'wins' => '0',
            'score' => '13'
        ),

    2 => array
        (
            'name' => 'Player 3',
            'wins' => '1',
            'score' => '12'
        ),

    3 => array
        (
            'name' => 'Player 4',
            'wins' => '2',
            'score' => '18'
        )

);

function winScore($a, $b)
{
    if ($a['wins'] == $b['wins']) {
		if ($a['score'] == $b['score']) {

			return 0;
		}
		return ($a['score'] < $b['score']) ? 1 : -1;
    }
    return ($a['wins'] < $b['wins']) ? 1 : -1;
}


usort($array, "winScore");

echo '<pre>';
print_r($array);

Open in new window

0
 

Author Closing Comment

by:duder78
ID: 34910178
Works like a champ, thanks alot!
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The greatest common divisor (gcd) of two positive integers is their largest common divisor. Let's consider two numbers 12 and 20. The divisors of 12 are 1, 2, 3, 4, 6, 12 The divisors of 20 are 1, 2, 4, 5, 10 20 The highest number among the c…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question