从数组得到三个最高值 [英] Get three highest values from the array

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

问题描述

我想从我的数组得到三个highes值,但应适当地键也进行排序。

我有这样的code:

 < PHP
$ A =阵列(130,1805,1337);
arsort($ A);
的print_r($ A);
?>

以上输出如下:

 阵列

    [1] => 1805
    [2] => 1337
    [0] => 130

其工作正常,但我想它additionaly向最低值从最高的按键排序。

例如:

 阵列

    [2] => 1805
    [1] => 1337
    [0] => 130

需要明确的是:我想它是由关键字排序:数组键2号将始终用于的最高的值,数组的键数0将一直用于的最低的值。

我怎么能这样做?

/让我知道,如果你不明白的东西。


解决方案

  rsort($数组);
$ TOP3 = array_reverse(array_slice($阵列,0,3));

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

I have this code:

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

The output of the above is following:

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.

Example:

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

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.

How can I do that?

/let me know if you don't understand something.

解决方案

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

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

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