如何在php数组中获取具有最高值的键[关闭]

如何在php数组中获取具有最高值的键[关闭]

问题描述:

I have this array...

Array (
[1168] => 46
[6973] => 27
[4585] => 24
[1224] => 23
[2010] => 20
[7514] => 19
[1167] => 17
[8349] => 7
[2476] => 7
[5313] => 7
[1208] => 2
)

And I need to get three keys (as values) with the highest values in the previous array:

Array (
[0] => 1168
[1] => 6973
[2] => 4585
)

Is the array always sorted in descending order? If not, you need to arsort($arr) it first:

$highest_keys = array_keys(array_slice($arr, 0, 3));

EDITED:

$a = array('foo' => 3, 'bar' => 2, 'fuz' => 1, 'baz' => 0);
$k = array_keys(array_slice($a, 0, 3));
print_r($k);

CodePad