Im looking to sort team stats by points and goals.
I thought I was onto something but Ive hit a spot
I get around.
Below Ive posted the file I pull from, the code and
two sets of results.
My intended end result would show:
Dk Blue 1 1 0 0 3 10
White 1 1 0 0 3 9
Lt Blue 1 1 0 0 3 8
Green 1 0 1 0 0 8
Purple 1 0 1 0 0 5
Yellow 1 0 1 0 0 4
teamstats.txt:
Dk_Blue 1 1 0 0 3 10
Green 1 0 1 0 0 8
Lt_Blue 1 1 0 0 3 8
Purple 1 0 1 0 0 5
White 1 1 0 0 3 9
Yellow 1 0 1 0 0 4
<?php
$stats = file('teamstats.txt');
foreach($stats as $line_number => $line){
$l = explode(" ",$line);
$newarray[$line_number]["team"] = $l[0];
$newarray[$line_number]["games"] = $l[1];
$newarray[$line_number]["wins"] = $l[2];
$newarray[$line_number]["loses"] = $l[3];
$newarray[$line_number]["ties"] = $l[4];
$newarray[$line_number]["points"] = $l[5];
$newarray[$line_number]["goals"] = $l[6];
}
foreach($newarray as $key => $row){
$team[$key] = $row['team'];
$games[$key] = $row['games'];
$win[$key] = $row['wins'];
$loses[$key] = $row['loses'];
$ties[$key] = $row['ties'];
$points[$key] = $row['points'];
$goals[$key] = $row['goals'];
}
array_multisort($points, SORT_DESC, $goals, SORT_DESC, $newarray);
$count = count($newarray);
print"<table>
<tr>
<th>Team</th>
<th>Games</th>
<th>Wins</th>
<th>Loses</th>
<th>Ties</th>
<th>Points</th>
<th>Goals</th>
</tr>";
for($i = 0; $i < $count; $i++){
print"
<tr>
<td>".$newarray[$i]["team"]."</td>
<td>".$newarray[$i]["games"]."</td>
<td>".$newarray[$i]["wins"]."</td>
<td>".$newarray[$i]["loses"]."</td>
<td>".$newarray[$i]["ties"]."</td>
<td>".$newarray[$i]["points"]."</td>
<td>".$newarray[$i]["goals"]."</td>
</tr>";
}
print"</table>";
?>
Results:
When I run ONLY Team, Points & Goals sorting on points and goals
I have this result:
Team Points Goals
Dk Blue 3 10
White 3 9
Lt Blue 3 8
Green 0 8
Purple 0 5
Yellow 0 4
When I sort on all as in the code above but still sorting only
points and goals I have this result:
Team Games Wins Loses Ties Points Goals
White 1 1 0 0 3 9
Lt_Blue 1 1 0 0 3 8
Dk_Blue 1 1 0 0 3 10
Green 1 0 1 0 0 8
Purple 1 0 1 0 0 5
Yellow 1 0 1 0 0 4
I found a function on http://www.the-art-of-web.com/php/sortarray/
that used this code
<?PHP
function compare_fullname($a, $b)
{
$retval = strnatcmp($a['lastname'], $b['lastname']);
if(!$retval) return strnatcmp($a['firstname'], $b['firstname']);
return $retval;
}
// sort alphabetically by firstname and lastname
usort($data, 'compare_fullname');
?>
to do something similar on names but, the problem with usort
which I found out very quickly is, "If two members compare as equal,
their relative order in the sorted array is undefined."- php.net
Any suggestion as to how to get these in the order I seek?
Thanks
Wade