从数组中获取三个最高值 [英] Get three highest values from the array

查看:33
本文介绍了从数组中获取三个最高值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的数组中获得三个最高值,但它也应该按键正确排序.

I want to get three highes values from my array, but it should be sorted properly by keys also.

我有这个代码:

<?php
$a = array(130, 1805, 1337);
arsort($a);
print_r($a);
?>

上面的输出如下:

Array
(
    [1] => 1805
    [2] => 1337
    [0] => 130
)

它工作正常,但我希望它另外将其键从最高值排序到最低值.

Its working fine, but I want it additionaly to sort its keys from the highest to the lowest value.

示例:

Array
(
    [2] => 1805
    [1] => 1337
    [0] => 130
)

要清楚:我希望它按键排序:数组键号 2 将始终用于 最高 值,数组键号 0 将始终用于 最低值.

To be clear: I want it be to sorted by the keys: array key number 2 will be always used for the highest value, array key number 0 will be always used for the lowest value.

我该怎么做?

/如果你不明白什么,请告诉我.

推荐答案

rsort($array);
$top3 = array_reverse(array_slice($array, 0, 3));

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

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