Jump to content

Sort Array


jandrews3

Recommended Posts

I'm sure this is an easy operation, but I suck at manipulating arrays. I have an array $team[0] through $team[7] all filled with integers and I need to know which field contains the 3 highest values. I'm using this to determine which teams in my classroom came in 1st, 2nd and 3rd place. Thanks.

Link to comment
https://forums.phpfreaks.com/topic/265798-sort-array/
Share on other sites

Yeah, I've found a bunch of possible functions for arrays, but my problem is that I'm totally inexperienced in using them. I suck at manipulating arrays. I'm not sure that sorting the array here is what I really need. What I need to do is find out which field, $team[0], $team[1], $team[2], $team[3], $team[4], $team[5], $team[6] or $team[7] has the highest value. Then I need to do the same to find out which has the second highest value, and then again for the third highest. Sorting may very well be what I need to do here, but I don't need the actual value ... I need to know which $team[] contains it. I can't figure out how to do this without a helluva lot of IF statements. I appreciate the help, but if rsort() or arsort() are possible solutions, I have no idea what the code would look like. Thanks.

Link to comment
https://forums.phpfreaks.com/topic/265798-sort-array/#findComment-1362128
Share on other sites

What about this would this help?

 


<?php 

$team0 = 1;
$team1 = 2;
$team2 = 3;
$teams = array(array('teamA' => $team0, 'teamB' => $team1, 'teamC' => $team2));
sort($teams);
while(list($key, $list) = each($teams)) {
							echo ' TeamA '.$list['teamA'] . ' TeamB '.$list['teamB'] . ' TeamC '.$list['teamC'] ;
}
?>

 

or am i on the wrong track ::)

Link to comment
https://forums.phpfreaks.com/topic/265798-sort-array/#findComment-1362130
Share on other sites

Or you could loop it

<?php 

$team0 = 1;
$team1 = 2;
$team2 = 3;
$teams = array(array('TeamA' => $team0, 'TeamB' => $team1, 'TeamC' => $team2));
sort($teams);
foreach($teams as $team){
while(list($key, $list) = each($team)) {

                      echo 'Team Results: ' . ' ' .$key . ' Points:  ' .$list . '<br>';
}}

?>

Link to comment
https://forums.phpfreaks.com/topic/265798-sort-array/#findComment-1362131
Share on other sites

I appreciate your help, but all either of those two scripts seemed to do was to display the data. I need to determine WHICH team had 1st, 2nd and 3rd place. That is to say, I need the script to determine that, so that extra credit points to my students can be automatically awarded.

Link to comment
https://forums.phpfreaks.com/topic/265798-sort-array/#findComment-1362133
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.