从数组得到三个最高值

编程入门 行业动态 更新时间:2024-10-10 15:25:11
本文介绍了从数组得到三个最高值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想从我的数组得到三个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));

更多推荐

从数组得到三个最高值

本文发布于:2023-11-29 09:55:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646063.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最高值   数组

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!