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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.