trim sorted array
final question for today... I've gotten far (thanks to all of you!), but i need to go even further.. I got a sorted array, and i need to have the upper highest values, so the i need to trim all the lower values under the highest...
value key
Boerée开发者_如何学运维 5
Bour 5
Linszen 4
de Wilde 3
Dingemans 3
Koelman 3
Tijssen 1
van der Meer 1
Bakker 1
de Haan 1
van Tricht 1
Nieman 1
Boer 1
Regards
It's sorted so you just make a loop for the amount of results that you want? Smells like homework so I'm not gonna post a full example. (Think I posted to much already).
$amount = 5;
for($i = 0; $i < amount; i++)
Ok, I think this should work, assuming I understand what you mean. I just felt like writing it too much to wait for your answer :)
function onlyTheStrong($array) {
$returnArray = array();
foreach ($array as $key => $value) {
if (!isset($max) || $value > $max) {
$max = $value;
$returnArray = array($key => $value);
} else if ($value == $max) {
$returnArray[$key] = $value;
}
}
return $returnArray;
}
It works for an unsorted array as well.
EDIT:
How about that, you can just use:
$top_authors = array_keys($authors, max($authors));
精彩评论