从数组中获取前5个最大值
问题描述:
我正在尝试从数值数组中获取前5个最大值...我尝试使用 rsort()
函数列出从最高到最高的数组值最低,但无法从结果中选择第1个。
i am trying to get the 1st 5 largest values from a numeric array...i have tried using the rsort()
function to list the array values from highest to lowest but cant get a way to pick the 1st 5 from the result.
答
使用 array_slice
:
$a = array ( 1, 3, 4, 2, 4, 5, 10, 7, 6, 8, 0 );
rsort($a);
$largest = array_slice($a, 0, 5);