sorting a mutli-dimensional array in PHP

Posted on 2011-02-16
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 :

    [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!
Question by:duder78
LVL 15

Accepted Solution

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


See code below.

$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>';

Open in new window


Author Closing Comment

ID: 34910178
Works like a champ, thanks alot!

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Article by: Nadia
Linear search (searching each index in an array one by one) works almost everywhere but it is not optimal in many cases. Let's assume, we have a book which has 42949672960 pages. We also have a table of contents. Now we want to read the content on p…
Iteration: Iteration is repetition of a process. A student who goes to school repeats the process of going to school everyday until graduation. We go to grocery store at least once or twice a month to buy products. We repeat this process every mont…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

777 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