note 99972 deleted from function.usort by danbrown

From: Date: Fri, 17 Sep 2010 14:33:33 +0000
Subject: note 99972 deleted from function.usort by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-172682@lists.php.net to get a copy of this message
Note Submitter: info at superio-design dot ch ---- <?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 ) ) */ ?>

« previous php.notes (#172682) next »