Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
查找数组的三个最大数(PHP)_Php_Arrays_Max - Fatal编程技术网

查找数组的三个最大数(PHP)

查找数组的三个最大数(PHP),php,arrays,max,Php,Arrays,Max,我需要知道如何找到数组中的三个最大数。目前我只能找到一个数组的最大数,但我想得到其中三个最大数: $avg = array(); $avg[20,10,30,50,80,90,220]; echo max($avg); 也许你能帮我个忙 // The array $avg = [20,10,30,50,80,90,220]; // remove duplicates if any $avg = array_unique($avg); // sort in descending order rs

我需要知道如何找到数组中的三个最大数。目前我只能找到一个数组的最大数,但我想得到其中三个最大数:

$avg = array();
$avg[20,10,30,50,80,90,220];
echo max($avg);
也许你能帮我个忙

// The array
$avg = [20,10,30,50,80,90,220];
// remove duplicates if any
$avg = array_unique($avg);
// sort in descending order
rsort($avg);
// get the first three elements
$avg = array_slice($avg, 0, 3);

你只是在这件事上打败了我几秒钟:我本来希望能有一艘客轮,但每次都是这样:谢谢你……行得通!:现在我明白了。你能告诉我怎样才能找到最低的三个数字吗?@johny把rsort改为sort