从数组中获取前5个最大值 [英] Get the 1st 5 largest values from an array

查看:419
本文介绍了从数组中获取前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);

这篇关于从数组中获取前5个最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆