paul_mcdonald Posted July 19, 2010 Share Posted July 19, 2010 Hello ... I'm fairly new to php. I have some code that calculates wins/losses for teams in a competition. The array contains team number, total wins, total losses. I need to figure out how to sort in order of wins (descending), loss (descending). Here is a sample array contents of $results_data in code below: Array ( [] => Array ( [293] => Array ( [Win] => 6 [Loss] => 0 ) [298] => Array ( [Win] => 6 [Loss] => 3 ) [297] => Array ( [Win] => 3 [Loss] => 3 ) [302] => Array ( [Win] => 2 [Loss] => 3 ) [304] => Array ( [Win] => 5 [Loss] => 3 ) [295] => Array ( [Win] => 6 [Loss] => 3 ) [299] => Array ( [Win] => 2 [Loss] => 3 ) [292] => Array ( [Win] => 1 [Loss] => 3 ) [301] => Array ( [Win] => 3 [Loss] => 3 ) [294] => Array ( [Win] => 4 [Loss] => 3 ) [306] => Array ( [Win] => 2 [Loss] => 3 ) [305] => Array ( [Win] => 1 [Loss] => 3 ) [300] => Array ( [Win] => 1 [Loss] => 3 ) [291] => Array ( [Win] => 3 [Loss] => 3 ) [303] => Array ( [Win] => 0 [Loss] => 3 ) [296] => Array ( [Win] => 0 [Loss] => 3 ) ) ) Here is the code that is used to display the list of teams and results: <?php if($results_data>0) { foreach($results_data as $r => $value) { if($r!="") { ?> <tr> <td colspan="3" width="100%" style="background-color:#3333CC; color:#FFFFFF; padding-left:20px;"><?php echo $r; ?></td> </tr> <?php } foreach($results_data[$r] as $z=> $result) { $team_name=$clsObj->Reetrive_team1_Name($z); ?> <tr> <td align="center"><?php echo $z; echo $team_name['team_skipName']; ?></td> <td align="center"><?php echo $results_data[$r][$z]["Win"]; ?></td> <td align="center"><?php echo $results_data[$r][$z]["Loss"]; ?> </td> </tr> <?php } } } else { ?> <tr> <td colspan="2">There have been no results for this event. </td> </tr> <?php }?> Thank you for your help! Quote Link to comment https://forums.phpfreaks.com/topic/208203-array-sorting-help/ Share on other sites More sharing options...
gwolgamott Posted July 19, 2010 Share Posted July 19, 2010 Out of multi-dimensional arrays.... with numerical keys... ugh... Another way may have been storing into two arrays so for every team there is two arrays, array_win and array_lose... so for like team 299 and team 250 I'd have had something like this.... $array_win[299]=3; $array_lose[299] = 5; $array_win[250]=6; $array_lose[250] = 2; Then I would have used arsort(); on which one you want to order by. For example say if you are sorting by wins do and arsort() on $array_win and use a foreach loop to print each element with you printing the matching element for the same key of the second array. arsort($array_win); foreach ($array_win as $key => $val) { echo "Wins for team ".$key.": ".$val."<br />"; echo "Loses for team "$key.": ".$array_lose[$key]."<br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/208203-array-sorting-help/#findComment-1088277 Share on other sites More sharing options...
paul_mcdonald Posted July 19, 2010 Author Share Posted July 19, 2010 So not possible with current setup? I didn't write the code, just trying to modify to add the sort. Quote Link to comment https://forums.phpfreaks.com/topic/208203-array-sorting-help/#findComment-1088278 Share on other sites More sharing options...
PFMaBiSmAd Posted July 19, 2010 Share Posted July 19, 2010 A) I can't help but wonder if this data is coming from a query, in which case you can simply do ORDER BY Win, Loss. And if this data is in a database and you are using a bunch of php code to come up with the Win/Loss values and the array you have shown, be advised that you can likely do this much easier directly in a query. B) If you must do this using an array in php, you can use array_multisort(), if you force the numerical [293] ... array indexes to be alpha-numeric associative names (so that php won't automatically convert them to numeric, in which case the resulting arrays will be re-indexed.) The following is the example from the php.net documentation for array_mulitsort(), modified for your example data - <?php $data = array(); $data['a293'] = Array ( 'Win' => 6, 'Loss' => 0 ); // the leading 'a' on the index forces these to be alpha associative index names $data['a298'] = Array ( 'Win' => 6, 'Loss' => 3 ); $data['a297'] = Array ( 'Win' => 3, 'Loss' => 3 ); $data['a302'] = Array ( 'Win' => 2, 'Loss' => 3 ); $data['a304'] = Array ( 'Win' => 5, 'Loss' => 3 ); $data['a295'] = Array ( 'Win' => 6, 'Loss' => 3 ); $data['a299'] = Array ( 'Win' => 2, 'Loss' => 3 ); $data['a292'] = Array ( 'Win' => 1, 'Loss' => 3 ); $data['a301'] = Array ( 'Win' => 3, 'Loss' => 3 ); $data['a294'] = Array ( 'Win' => 4, 'Loss' => 3 ); $data['a306'] = Array ( 'Win' => 2, 'Loss' => 3 ); $data['a305'] = Array ( 'Win' => 1, 'Loss' => 3 ); $data['a300'] = Array ( 'Win' => 1, 'Loss' => 3 ); $data['a291'] = Array ( 'Win' => 3, 'Loss' => 3 ); $data['a303'] = Array ( 'Win' => 0, 'Loss' => 3 ); $data['a296'] = Array ( 'Win' => 0, 'Loss' => 3 ); // Obtain a list of columns foreach ($data as $key => $row) { $win["$key"] = $row['Win']; $loss["$key"] = $row['Loss']; } // Sort the data with win descending, loss descending // Add $data as the last parameter, to sort by the common key array_multisort($win, SORT_DESC, $loss, SORT_DESC, $data); echo "Sorted data array:<pre>",print_r($data,true),"</pre>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/208203-array-sorting-help/#findComment-1088297 Share on other sites More sharing options...
PFMaBiSmAd Posted July 19, 2010 Share Posted July 19, 2010 Slight correction to the above, should this be from a query - ORDER BY Win DESC, Loss DESC Quote Link to comment https://forums.phpfreaks.com/topic/208203-array-sorting-help/#findComment-1088337 Share on other sites More sharing options...
paul_mcdonald Posted July 20, 2010 Author Share Posted July 20, 2010 For reference .... here is the complete code for the page I'm working with. I don't think the sql sort would work in this case. <?php include_once("admin/header/header.php"); $clsObj=new events(); //$clsobject=new adduser(); $curdate= date("Y-m-d"); $current_events=$clsObj->retrive_curreent_schedule_events($curdate); if(isset($_GET["b_id"])) { $event_details=$clsObj->Retriv_event_name($_GET["b_id"]); $date_s=$event_details['B_startDate']; $year=substr($date_s,0,4); $mon=substr($date_s,5,2); $day=substr($date_s,8,2); $start_date=$mon."/".$day."/".$year; $date_e=$event_details['B_endDate']; $year_e=substr($date_e,0,4); $mon_e=substr($date_e,5,2); $day_e=substr($date_e,8,2); $end_date=$mon_e."/".$day_e."/".$year_e; $club_id=$event_details['B_club_id']; $club_name=$clsObj->Find_clubNameAccordingtievent_id($club_id); $record_all=$clsObj->Retrive_schedule_record_accrodingTOB_ID($_GET["b_id"]); if($record_all>0) { $back_div=""; $same_group=0; $total_teams=array(); $main_counter=0; $counter=0; $youcanwin=array(); $winner_result=array(); $results_data=array(); $result_counter=0; $groups=array(); foreach($record_all as $t=>$main_groups) { $groups[$t]=$record_all[$t]["division"]; } $main_groups=array_count_values($groups); $tot_group =array(); $group_counter=0; foreach($main_groups as $a=>$groups_det) { $tot_group[$group_counter]=$a; $group_counter++; } foreach($record_all as $x=>$value) { $current_div=$record_all[$x]["division"]; if($back_div!=$current_div) { $same_group=1; $main_counter++; } if($main_counter==0) { $total_teams[$main_counter][$counter]=$record_all[$x]["team1_id"]; $youcanwin[$main_counter][$counter]=$record_all[$x]["team1_result"]; $counter++; $total_teams[$main_counter][$counter]=$record_all[$x]["team2_id"]; $youcanwin[$main_counter][$counter]=$record_all[$x]["team2_result"]; $counter++; }else { $total_teams[$main_counter][$counter]=$record_all[$x]["team1_id"]; $youcanwin[$main_counter][$counter]=$record_all[$x]["team1_result"]; $counter++; $total_teams[$main_counter][$counter]=$record_all[$x]["team2_id"]; $youcanwin[$main_counter][$counter]=$record_all[$x]["team2_result"]; $counter++; } $back_div=$record_all[$x]["division"]; } foreach($total_teams as $x=>$value) { $tot_teams=$total_teams[$x]; $tot_result=$youcanwin[$x]; $final_array=array(); foreach($tot_teams as $t => $values) { $key_val=$tot_teams[$t]; $to_res=$tot_result[$t]; if($to_res=="Win") { $win=1; $loss=0; }else if($to_res=="Loss") { $win="0"; $loss="1"; }else { $win="0"; $loss="0"; } if (array_key_exists($key_val,$final_array)) { if($to_res=="Win") { $win=(int)$final_array[$key_val]["Win"] + 1; $loss=(int)$final_array[$key_val]["Loss"] + 0; } if($to_res=="Loss") { $win=(int)$final_array[$key_val]["Win"] + 0 ; $loss=(int)$final_array[$key_val]["Loss"] + 1; } if($to_res=="Not completed") { $win=(int)$final_array[$key_val]["Win"] + 0; $loss=(int)$final_array[$key_val]["Loss"] + 0; } $final_array[$key_val]["Win"]=$win; $final_array[$key_val]["Loss"]=$loss; }else { $final_array[$key_val]["Win"]=$win; $final_array[$key_val]["Loss"]=$loss; } } $results_data[$tot_group[$result_counter]]=$final_array; $result_counter++; } } // print_r($schedule); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title><?php echo $event_details['B_title'];?> :: Results</title> <link href="css/styles.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="script-css/client.js"></script> </head> <body> <div id="wraper"> <div id="header"> <?php include_once("header_spiel.php"); ?> </div> <div id="body_container"> <div id="page1"> <table width="100%" border="0" class="font12" cellspacing="0" cellpadding="0"> <tr> <td height="40" valign="top"><h2><?php echo $event_details['B_title']; ?> - <?php echo $club_name['club_name']; ?> </h2><?php echo $start_date; ?> - <?php echo $end_date; ?><hr /></td> </tr> <tr> <td style=" padding-top:10px"><h3>Results</h3></td> </tr> <tr> <td height="25"></td> </tr> <tr> <td valign="top"> <table border="0" width="80%" align="left" > <tr> <td align="center">Skip</td> <td align="center">Win</td> <td align="center">Loss</td> </tr> <?php if($results_data>0) { foreach($results_data as $r => $value) { if($r!="") { ?> <tr> <td colspan="3" width="100%" style="background-color:#3333CC; color:#FFFFFF; padding-left:20px;"><?php echo $r; ?></td> </tr> <?php } foreach($results_data[$r] as $z=> $result) { $team_name=$clsObj->Reetrive_team1_Name($z); ?> <tr> <td align="center"><?php echo $z; echo $team_name['team_skipName']; ?></td> <td align="center"><?php echo $results_data[$r][$z]["Win"]; ?></td> <td align="center"><?php echo $results_data[$r][$z]["Loss"]; ?> </td> </tr> <?php } } } else { ?> <tr> <td colspan="2">There have been no results for this event. </td> </tr> <?php }?> </table> </td> </tr> <tr> <td valign="top" class="font12"><p> </p></td> </tr> <tr> <td valign="top"><?php include_once("footer_top.php"); ?></td> </tr> <tr> <td valign="top"> </td> </tr> </table> </div> <?php include_once("footer.php"); ?> </div> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/208203-array-sorting-help/#findComment-1088520 Share on other sites More sharing options...
PFMaBiSmAd Posted July 20, 2010 Share Posted July 20, 2010 Without knowing the table definition(s), seeing some sample data, and knowing what your database class methods are, you are right, it is probably not possible to come up with a solution that does this in a single query. But based on what your code does tell, your multiple calls to get the $club_name and $record_all would normally be handled by joining tables and adding the wins/losses would be handled by database count functions directly in the query. Quote Link to comment https://forums.phpfreaks.com/topic/208203-array-sorting-help/#findComment-1088682 Share on other sites More sharing options...
gwolgamott Posted July 20, 2010 Share Posted July 20, 2010 So not possible with current setup? I didn't write the code, just trying to modify to add the sort. It's possible, just create a function that splits the multidimensional array into two arrays a win array and loss array... sort accordingly as I explained earlier. Then at the end of the function return the value in a new multidimensional array sorted accordingly and replace the $results_data with the array returned from the function. Too long to write for the a help forum, sorry lack of time... but essentially in the function you will have sent two things... the $results_data array and a flag of win or loss for ordering. In the function you will split the array into two arrays... for loops while loops foreach whatever you prefer... but get the info so the $array_win has the keys of the teams and a value of the win number... do same for the $array_lose... Do my script for sorting depending on the flag sent to the function for whether it's win or loss... rebuild the original multi-dimensional array using the same method you took it apart so its in order the way you want... Return that array.... Clear old array and make it equal to new array. Quote Link to comment https://forums.phpfreaks.com/topic/208203-array-sorting-help/#findComment-1088699 Share on other sites More sharing options...
katierosy Posted July 22, 2010 Share Posted July 22, 2010 Hope this example will make it easy and clear to accomplish the task as mentioned in this forum. <?php $str1['win']=5; $str1['loss']=6; $str[]=$str1; $str1['win']=1; $str1['loss']=2; $str[]=$str1; $str1['win']=4; $str1['loss']=3; $str[]=$str1; $str1['win']=8; $str1['loss']=2; $str[]=$str1; print_r($str); foreach($str as $key => $row) { $win[$key] = $row['win']; $loss[$key] = $row['loss']; } array_multisort($win,SORT_DESC,$loss,SORT_ASC,$str); print_r($str); ?> Quote Link to comment https://forums.phpfreaks.com/topic/208203-array-sorting-help/#findComment-1089444 Share on other sites More sharing options...
PFMaBiSmAd Posted July 22, 2010 Share Posted July 22, 2010 Since that code doesn't maintain WHO the win/loss values belong to and it is using SORT_ASC for the losses (the OP asked for DESC for both), Buzzzzzzzzz, non-working solution. Edit: in fact what gwolgamott has been posting is incomplete as well. The goal is to sort both wins and losses at the same time, so that within the same number of wins, the losses are sorted as well. If anyone reading this thread looks at the tested array_multisort() code that I posted above, it does sort by both wins DESC and losses DESC and it maintains who the win/loss values belong to. Quote Link to comment https://forums.phpfreaks.com/topic/208203-array-sorting-help/#findComment-1089674 Share on other sites More sharing options...
gwolgamott Posted July 22, 2010 Share Posted July 22, 2010 Edit: in fact what gwolgamott has been posting is incomplete as well. The goal is to sort both wins and losses at the same time, so that within the same number of wins, the losses are sorted as well. Yes it is incomplete in that sense. I took that he wanted an option to sort in descending for either wins or for loses as in a link, button whatever. But I see, however it is not at all hard to do if for both to display on separate rows. This is simplified here //$Teams is your array with the team number arrays for win / loss foreach($Teams as $key => $value) { $array_win[$key] = $value['Win']; $array_loss[$key]=$value['Loss']; } arsort($array_win); arsort($array_loss); //now print the arrays however you want you have both sorted highest to lowest here Here's an example of how to do this with explanation and real output for the sample data below. //Lets say the array $Teams is something like this /* Array ( [293] => Array ( [Win] => 6 [Loss] => 0 ) [298] => Array ( [Win] => 6 [Loss] => 3 ) [297] => Array ( [Win] => 3 [Loss] => 3 ) [302] => Array ( [Win] => 2 [Loss] => 3 ) [304] => Array ( [Win] => 5 [Loss] => 3 ) [295] => Array ( [Win] => 6 [Loss] => 3 ) [299] => Array ( [Win] => 2 [Loss] => 3 ) [292] => Array ( [Win] => 1 [Loss] => 3 ) [301] => Array ( [Win] => 3 [Loss] => 3 ) [294] => Array ( [Win] => 4 [Loss] => 3 ) [306] => Array ( [Win] => 2 [Loss] => 3 ) [305] => Array ( [Win] => 1 [Loss] => 3 ) [300] => Array ( [Win] => 1 [Loss] => 3 ) [291] => Array ( [Win] => 3 [Loss] => 3 ) [303] => Array ( [Win] => 0 [Loss] => 3 ) [296] => Array ( [Win] => 0 [Loss] => 3 ) ) */ foreach($Teams as $key => $value) { $array_win[$key] = $value['Win']; $array_loss[$key]=$value['Loss']; } // Will now have this respectively for win & loss array /* $array_win is: Array ( [293] => 6 [298] => 6 [297] => 3 [302] => 2 [304] => 5 [295] => 6 [299] => 2 [292] => 1 [301] => 3 [294] => 4 [306] => 2 [305] => 1 [300] => 1 [291] => 3 [303] => 0 [296] => 0 ) $array_loss is: Array ( [293] => 0 [298] => 3 [297] => 3 [302] => 3 [304] => 3 [295] => 3 [299] => 3 [292] => 3 [301] => 3 [294] => 3 [306] => 3 [305] => 3 [300] => 3 [291] => 3 [303] => 3 [296] => 3 ) */ arsort($array_win); //use asort($array_win); for lowest to highest instead arsort($array_loss); //use asort($array_loss); for lowest to highest //will sort each array from highest to lowest.... //Will now be /* $array_win is now: Array ( [293] => 6 [295] => 6 [298] => 6 [304] => 5 [294] => 4 [291] => 3 [297] => 3 [301] => 3 [306] => 2 [299] => 2 [302] => 2 [300] => 1 [292] => 1 [305] => 1 [296] => 0 [303] => 0 ) $array_loss is now: Array ( [305] => 3 [306] => 3 [294] => 3 [300] => 3 [291] => 3 [296] => 3 [303] => 3 [301] => 3 [292] => 3 [297] => 3 [298] => 3 [302] => 3 [304] => 3 [299] => 3 [295] => 3 [293] => 0 ) */ //Now print the arrays however you want. You have two arrays sorted from highest to lowest from the original array. Hope this helps. sorry if mislead you earlier. Quote Link to comment https://forums.phpfreaks.com/topic/208203-array-sorting-help/#findComment-1089729 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.