chiprivers Posted March 3, 2007 Share Posted March 3, 2007 I am working with football results and currently trying to produce a league table display. Each football fixture is entered as a seperate database record and I have so far written the script to analyse these to obtain the stats required and enter them into a multi-diemsional array. I am left with the array as below: $stats[$x][pd] = value // games played $stats[$x][hw] = value // home wins $stats[$x][hd] = value // home draws $stats[$x][hl] = value // home loses $stats[$x]hf] = value // home goals for // etc etc $stats[$x][pts] = value // points in the above, $x increments for each team entered into the array. I need to use array_multisort to sort the array so that the teams stats are reordered by the final points value in descending order. I have looked at a couple of resources on the net but can't get my head around using this. May be there is an easier way? ANy suggestions much appreciated. Link to comment https://forums.phpfreaks.com/topic/40990-solved-multi-dimensional-arrays-and-array_multisort/ Share on other sites More sharing options...
Barand Posted March 3, 2007 Share Posted March 3, 2007 uasort() with custom sort function <?php $stats = array ( 'manutd' => array ( 'pd' => 6, 'hw' => 2, 'hd' => 1, 'hl' => 0, 'hf' => 5, 'pts' => 10 ), 'mancty' => array ( 'pd' => 6, 'hw' => 1, 'hd' => 1, 'hl' => 0, 'hf' => 3, 'pts' => 4 ), 'arsenal' => array ( 'pd' => 5, 'hw' => 1, 'hd' => 1, 'hl' => 0, 'hf' => 4, 'pts' => 8 ), 'chelsea' => array ( 'pd' => 6, 'hw' => 2, 'hd' => 2, 'hl' => 0, 'hf' => 4, 'pts' => 9 ) ); function sort_by_pts ($a, $b) { if ($a['pts'] == $b['pts']) return 0; return ($a['pts'] > $b['pts']) ? -1 : 1; } uasort ($stats, 'sort_by_pts') ; foreach ($stats as $team => $data) { echo "$team : {$data['pts']} pts<br>"; } ?> Link to comment https://forums.phpfreaks.com/topic/40990-solved-multi-dimensional-arrays-and-array_multisort/#findComment-198489 Share on other sites More sharing options...
chiprivers Posted March 3, 2007 Author Share Posted March 3, 2007 I have applied what you said and it works to a point. First problem: I am looping through the script i have prepared for each league. It works fine the first time through the loop but then the script fails on the second iteration of the loop! Second problem: Is it possible to amend this so that it would sort by ['pts'] descending then by ['gd'] descending for teams on equal ['pts']? Link to comment https://forums.phpfreaks.com/topic/40990-solved-multi-dimensional-arrays-and-array_multisort/#findComment-198655 Share on other sites More sharing options...
Barand Posted March 3, 2007 Share Posted March 3, 2007 Second <?php function sort_by_pts ($a, $b) { if ($a['pts'] == $b['pts']) { if ($a['gd'] == $b['gd']) return 0; return ($a['gd'] > $b['gd']) ? -1 : 1; } return ($a['pts'] > $b['pts']) ? -1 : 1; } ?> Link to comment https://forums.phpfreaks.com/topic/40990-solved-multi-dimensional-arrays-and-array_multisort/#findComment-198656 Share on other sites More sharing options...
chiprivers Posted March 3, 2007 Author Share Posted March 3, 2007 Excellent, that has sorted out the sorting problem , but I still have the problem with the script stopping on the second iteration of the loop. My scipt is below if it helps: <?php // get all leagues $get_leagues = "SELECT l.leagueID, l.league FROM leagues AS l ORDER BY leagueID ASC"; $got_leagues = mysql_query($get_leagues); // one league at a time while ($leagues = mysql_fetch_array($got_leagues)) { // display league name echo "<h4>".$leagues['league']."</h4>\n"; // get all teams for this league $get_teams = "SELECT t.* FROM teams AS t WHERE leagueID = ".$leagues['leagueID']; $got_teams = mysql_query($get_teams); // one team at a time while ($teams = mysql_fetch_array($got_teams)){ // set counter $x++; // get stats for this team for games played between set start and end dates $stats[$x][team] = $teams['team']; // get number of games played $get_gp = "SELECT COUNT(*) AS gp FROM fixtures AS f WHERE (f.home = ".$teams['teamID']." OR f.away = ".$teams['teamID'].") AND played = 1 AND fixture >= '".$start_date."' AND fixture <= '".$end_date."' "; $got_gp = mysql_query($get_gp); $gp = mysql_fetch_array($got_gp); $stats[$x][gp] = $gp['gp']; // get home wins $get_hw = "SELECT COUNT(*) AS hw FROM fixtures AS f WHERE f.home = ".$teams['teamID']." AND played = 1 AND fixture >= '".$start_date."' AND fixture <= '".$end_date."' AND f.h_goals > f.a_goals"; $got_hw = mysql_query($get_hw); $hw = mysql_fetch_array($got_hw); $stats[$x][hw] = $hw['hw']; // get home draws $get_hd = "SELECT COUNT(*) AS hd FROM fixtures AS f WHERE f.home = ".$teams['teamID']." AND played = 1 AND fixture >= '".$start_date."' AND fixture <= '".$end_date."' AND f.h_goals = f.a_goals"; $got_hd = mysql_query($get_hd); $hd = mysql_fetch_array($got_hd); $stats[$x][hd] = $hd['hd']; // get home loses $get_hl = "SELECT COUNT(*) AS hl FROM fixtures AS f WHERE f.home = ".$teams['teamID']." AND played = 1 AND fixture >= '".$start_date."' AND fixture <= '".$end_date."' AND f.h_goals < f.a_goals"; $got_hl = mysql_query($get_hl); $hl = mysql_fetch_array($got_hl); $stats[$x][hl] = $hl['hl']; // get home for $get_hf = "SELECT SUM(f.h_goals) AS hf FROM fixtures AS f WHERE f.home = ".$teams['teamID']." AND played = 1 AND fixture >= '".$start_date."' AND fixture <= '".$end_date."'"; $got_hf = mysql_query($get_hf); $hf = mysql_fetch_array($got_hf); $stats[$x][hf] = $hf['hf']; // get home against $get_ha = "SELECT SUM(f.a_goals) AS ha FROM fixtures AS f WHERE f.home = ".$teams['teamID']." AND played = 1 AND fixture >= '".$start_date."' AND fixture <= '".$end_date."'"; $got_ha = mysql_query($get_ha); $ha = mysql_fetch_array($got_ha); $stats[$x][ha] = $ha['ha']; // get away wins $get_aw = "SELECT COUNT(*) AS aw FROM fixtures AS f WHERE f.away = ".$teams['teamID']." AND played = 1 AND fixture >= '".$start_date."' AND fixture <= '".$end_date."' AND f.h_goals < f.a_goals"; $got_aw = mysql_query($get_aw); $aw = mysql_fetch_array($got_aw); $stats[$x][aw] = $aw['aw']; // get away draws $get_ad = "SELECT COUNT(*) AS ad FROM fixtures AS f WHERE f.away = ".$teams['teamID']." AND played = 1 AND fixture >= '".$start_date."' AND fixture <= '".$end_date."' AND f.h_goals = f.a_goals"; $got_ad = mysql_query($get_ad); $ad = mysql_fetch_array($got_ad); $stats[$x][ad] = $ad['ad']; // get away loses $get_al = "SELECT COUNT(*) AS al FROM fixtures AS f WHERE f.away = ".$teams['teamID']." AND played = 1 AND fixture >= '".$start_date."' AND fixture <= '".$end_date."' AND f.h_goals > f.a_goals"; $got_al = mysql_query($get_al); $al = mysql_fetch_array($got_al); $stats[$x][al] = $al['al']; // get away for $get_af = "SELECT SUM(f.a_goals) AS af FROM fixtures AS f WHERE f.away = ".$teams['teamID']." AND played = 1 AND fixture >= '".$start_date."' AND fixture <= '".$end_date."'"; $got_af = mysql_query($get_af); $af = mysql_fetch_array($got_af); $stats[$x][af] = $af['af']; // get away against $get_aa = "SELECT SUM(f.h_goals) AS aa FROM fixtures AS f WHERE f.away = ".$teams['teamID']." AND played = 1 AND fixture >= '".$start_date."' AND fixture <= '".$end_date."'"; $got_aa = mysql_query($get_aa); $aa = mysql_fetch_array($got_aa); $stats[$x][aa] = $aa['aa']; // calculate goal difference $stats[$x][gd] = ($stats[$x][hf] + $stats[$x][af]) - ($stats[$x][ha] + $stats[$x][aa]); // calculate points $stats[$x][pts] = (($stats[$x][hw] + $stats[$x][aw]) * 3) + (($stats[$x][hd] + $stats[$x][ad]) * 1); } // sort multidimensional array in descending order of $stats[$x][pts] function sort_by_pts ($a, $b) { if ($a['pts'] == $b['pts']) { if ($a['gd'] == $b['gd']) return 0; return ($a['gd'] > $b['gd']) ? -1 : 1; } return ($a['pts'] > $b['pts']) ? -1 : 1; } uasort ($stats, 'sort_by_pts') ; // start table echo "<table>\n"; // one team at a time foreach ($stats as $key => $data) { // start row echo "<tr>\n"; // show team name echo "<td class=\"home_cell\">".$data['team']."</td>\n"; // show stats echo "<td class=\"score_cell\">".$data['gp']."</td>"; echo "<td class=\"score_cell\">".$data['hw']."</td>"; echo "<td class=\"score_cell\">".$data['hd']."</td>"; echo "<td class=\"score_cell\">".$data['hl']."</td>"; echo "<td class=\"score_cell\">".$data['hf']."</td>"; echo "<td class=\"score_cell\">".$data['ha']."</td>"; echo "<td class=\"score_cell\">".$data['aw']."</td>"; echo "<td class=\"score_cell\">".$data['ad']."</td>"; echo "<td class=\"score_cell\">".$data['al']."</td>"; echo "<td class=\"score_cell\">".$data['af']."</td>"; echo "<td class=\"score_cell\">".$data['aa']."</td>"; echo "<td class=\"score_cell\">".$data['gd']."</td>"; echo "<td class=\"score_cell\">".$data['pts']."</td>"; // close row echo "</tr>\n"; } // close table echo "</table>\n"; // empty array unset($stats); } ?> Link to comment https://forums.phpfreaks.com/topic/40990-solved-multi-dimensional-arrays-and-array_multisort/#findComment-198661 Share on other sites More sharing options...
Barand Posted March 3, 2007 Share Posted March 3, 2007 Try moving the function definition to somewhere outside the loops. Itmay be objecting to multiple definition of the function. Do you have any error reporting switched on? Link to comment https://forums.phpfreaks.com/topic/40990-solved-multi-dimensional-arrays-and-array_multisort/#findComment-198667 Share on other sites More sharing options...
chiprivers Posted March 3, 2007 Author Share Posted March 3, 2007 Thats it, declared the function at the beginning of the script and it works fine now. Thanks Link to comment https://forums.phpfreaks.com/topic/40990-solved-multi-dimensional-arrays-and-array_multisort/#findComment-198677 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.