jandrews3 Posted July 17, 2012 Share Posted July 17, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/265798-sort-array/ Share on other sites More sharing options...
Pikachu2000 Posted July 17, 2012 Share Posted July 17, 2012 rsort Quote Link to comment https://forums.phpfreaks.com/topic/265798-sort-array/#findComment-1362046 Share on other sites More sharing options...
btherl Posted July 17, 2012 Share Posted July 17, 2012 Or arsort() if you need to know the index of the 3 highest values, rather then the 3 highest values themselves. Quote Link to comment https://forums.phpfreaks.com/topic/265798-sort-array/#findComment-1362048 Share on other sites More sharing options...
jandrews3 Posted July 17, 2012 Author Share Posted July 17, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/265798-sort-array/#findComment-1362128 Share on other sites More sharing options...
raptor30506090 Posted July 17, 2012 Share Posted July 17, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/265798-sort-array/#findComment-1362130 Share on other sites More sharing options...
raptor30506090 Posted July 17, 2012 Share Posted July 17, 2012 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>'; }} ?> Quote Link to comment https://forums.phpfreaks.com/topic/265798-sort-array/#findComment-1362131 Share on other sites More sharing options...
jandrews3 Posted July 17, 2012 Author Share Posted July 17, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/265798-sort-array/#findComment-1362133 Share on other sites More sharing options...
Barand Posted July 17, 2012 Share Posted July 17, 2012 If your array only contains the integer points values, how do you know which teams they belong to? Quote Link to comment https://forums.phpfreaks.com/topic/265798-sort-array/#findComment-1362174 Share on other sites More sharing options...
jandrews3 Posted July 17, 2012 Author Share Posted July 17, 2012 $team[0] is team 1, it's value is the points team 1 has earned. And so on. Quote Link to comment https://forums.phpfreaks.com/topic/265798-sort-array/#findComment-1362176 Share on other sites More sharing options...
Barand Posted July 17, 2012 Share Posted July 17, 2012 try <?php $teams = array(50,45,20,35,80,55,25); arsort($teams); for ($pos=1; $pos <= 3; ++$pos) { list ($team, $pts) = each($teams); echo "Pos $pos: Team " . ++$team . " with $pts points<br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/265798-sort-array/#findComment-1362183 Share on other sites More sharing options...
jandrews3 Posted July 17, 2012 Author Share Posted July 17, 2012 @Barand - THANK YOU!!! That piece of code helped me to do exactly what I needed. Quote Link to comment https://forums.phpfreaks.com/topic/265798-sort-array/#findComment-1362229 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.