note 99972 added to function.usort
| From: | info at superio-design dot ch | Date: | Fri, 17 Sep 2010 13:57:09 +0000 |
| Subject: | note 99972 added to function.usort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-172674@lists.php.net to get a copy of this message | ||
<?php
//Function to sort a ranking
//$array[1]['name'] = Players Name
//$array[1]['points'] = Points;
//$array[1]['gs'] = Goals shot;
//$array[1]['gd'] = Goal difference;
//
//sort priorities
//1.: Points
//2.: Goal Difference
//3.: Goals shot
$array[0]['name'] = 'Player 1';
$array[0]['points'] = '6';
$array[0]['gs'] = '12';
$array[0]['gd'] = '5';
$array[1]['name'] = 'Player 2';
$array[1]['points'] = '3';
$array[1]['gs'] = '5';
$array[1]['gd'] = '1';
$array[2]['name'] = 'Player 3';
$array[2]['points'] = '6';
$array[2]['gs'] = '12';
$array[2]['gd'] = '7';
function ranking($a, $b){
if ($a['points'] == $b['points']) {
//same goal difference
if ($a['gd'] == $b['gd']) {
//more scored goals
return ($b['gs'] - $a['gs']);
}
//different goal difference
else {
return($b['gd'] - $a['gd']);
}
}
else {
return($b['points'] - $a['points']);
}
}
usort($array, 'ranking');
echo "<pre>";
print_r ($array);
echo "</pre>";
/*
Array
(
[0] => Array
(
[name] => Player 3
[points] => 6
[gs] => 12
[gd] => 7
)
[1] => Array
(
[name] => Player 1
[points] => 6
[gs] => 12
[gd] => 5
)
[2] => Array
(
[name] => Player 2
[points] => 3
[gs] => 5
[gd] => 1
)
)
*/
?>
----
Server IP: 62.2.182.223
Probable Submitter: 159.29.40.10
----
Manual Page -- http://www.php.net/manual/en/function.usort.php
Edit -- https://master.php.net/note/edit/99972
Del: integrated -- https://master.php.net/note/delete/99972/integrated
Del: useless -- https://master.php.net/note/delete/99972/useless
Del: bad code -- https://master.php.net/note/delete/99972/bad+code
Del: spam -- https://master.php.net/note/delete/99972/spam
Del: non-english -- https://master.php.net/note/delete/99972/non-english
Del: in docs -- https://master.php.net/note/delete/99972/in+docs
Del: other reasons-- https://master.php.net/note/delete/99972
Reject -- https://master.php.net/note/reject/99972
Search -- https://master.php.net/manage/user-notes.php