# Sort Array

## 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.

##### Share on other sites

Or arsort() if you need to know the index of the 3 highest values, rather then the 3 highest values themselves.

##### 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.

##### Share on other sites

```
<?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

##### 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>';
}}

?>
```

##### 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.

##### Share on other sites

If your array only contains the integer points values, how do you know which teams they belong to?

##### Share on other sites

\$team[0] is team 1, it's value is the points team 1 has earned. And so on.

##### Share on other sites

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 />";
}
?>```

##### Share on other sites

@Barand - THANK YOU!!! That piece of code helped me to do exactly what I needed.

##### Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

## Join the conversation

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

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.