I have the following array setup called $players:

Array(
    [4153234567] => Array(
        [name] => JohnnyAppleSeed
        [rating] => 00
    )
    [4807173711] => Array(
        [name] => admin
        [rating] => 6000
    )
    [4801234562] => Array(
        [name] => 4801234562
        [rating] = > 00
    )
)

I need to sort this array and echo:

$name of person with highest rating
$name of person with lowest rating

then remove these people from the array im looking at and repeat till I have moved through everyone.

Any ideas?

link|improve this question
Possible duplicate Sorting a multidimensional array in PHP?. – hakre Oct 14 '11 at 1:24
feedback

2 Answers

up vote 4 down vote accepted

Try usort

 usort($players, "player_sort");

 function player_sort($a,$b) {
      return $a['rating']>$b['rating'];
 }

http://www.php.net/manual/en/function.usort.php

Once sorted, you can take the first and last element to get the highest and lowest.

link|improve this answer
3  
Man, beat me to it because I took the time to format his code first :) – Levi Morrison Oct 14 '11 at 1:14
Cheers and +1 for you. :) – Jimtronic Oct 14 '11 at 1:18
Here's a link with a more complete implementation: codepad.org/y9Q5bpuQ – Levi Morrison Oct 14 '11 at 1:24
Thank you! :D Arrays confuse me! – John Garretson Oct 14 '11 at 1:26
feedback

Given the data you provided, you're actually going to need to use uasort -- that is unless the keys of the array (ie.4153234567) don't matter to you. Otherwise the principle and sorting routine is the same.

Also to remove items you would use unset(). If you save the keys in to $first, $last, then

unset($players[$first]);
unset($players[$last]);
link|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.