iJamesPHP2 - 1 year ago 89
PHP Question

# Team ranking system design

I have a league system in which there are teams. The teams have a certain amount of wins and a certain amount of losses. Is there an equation I could use to rank them based on their win/losses other than simply dividing the wins by the losses and ordering those numbers,

Thanks,
James

Note : Exactly I still don't get your Question much clearly but just as far as I know that Wins are to be considered in Positive Integers while the losses are to be considered in negative integers.

Here is some code which can pretty much do your job :

``````<?php
\$teams_details = array(
array('team_name' => "Team 1", 'wins' => "3", 'losses' => "0" ),
array('team_name' => "Team 2", 'wins' => "2", 'losses' => "1" ),
array('team_name' => "Team 3", 'wins' => "1", 'losses' => "2" ),
array('team_name' => "Team 4", 'wins' => "0", 'losses' => "3" ),
array('team_name' => "Team 5", 'wins' => "3", 'losses' => "0" ),
array('team_name' => "Team 6", 'wins' => "2", 'losses' => "1" ) );
\$ranking_array = array();
for(\$i=0;\$i<count(\$teams_details);\$i++)
{
\$team_name = \$teams_details[\$i]['team_name'];
\$rank = \$teams_details[\$i]['wins'] - \$teams_details[\$i]['losses'];
\$team_array = array('team_name' => \$team_name, 'rank' => \$rank );
\$ranking_array[] = \$team_array;
}

function invenDescSort(\$item1,\$item2)
{
if (\$item1['rank'] == \$item2['rank']) return 0;
return (\$item1['rank'] < \$item2['rank']) ? 1 : -1;
}
usort(\$ranking_array,'invenDescSort');
for(\$i=0;\$i<count(\$ranking_array);\$i++) {
\$ranking_array[\$i]['rank'] = \$i+1;
}
echo "<pre>";
print_r(\$ranking_array);
echo "</pre>";
?>
``````

OUTPUT OF THE CODE :

``````Array
(
[0] => Array
(
[team_name] => Team 1
[rank] => 1
)

[1] => Array
(
[team_name] => Team 5
[rank] => 2
)

[2] => Array
(
[team_name] => Team 6
[rank] => 3
)

[3] => Array
(
[team_name] => Team 2
[rank] => 4
)

[4] => Array
(
[team_name] => Team 3
[rank] => 5
)

[5] => Array
(
[team_name] => Team 4
[rank] => 6
)

)
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download