从一个数组,PHP得到3个最低值

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

所以我知道 MIN()可以让我从一个数组中的最低值,但我需要一个数组的3最低值。

假设我的数组被命名为 $ myArray的:

我用 ASORT($ myArray的)我的数组,然后的print_r($ myArray的)输出这样的:

阵列(    [137] => 4.90416668118    [135] => 7.1203544474    [18] => 7.2476262434    [81] => 8.37903400152    [33] => 9.1074567001    [4] => 9.90788482793    [138] => 10.2493339987    [5] => 11.6024401676    [63] ......等等,直到    [124] => 8727.73285117    [153] => 8727.73285117    [117] => 8727.73285117)

如何获得第3的值或X值第一我需要...

我应该已经指定:是否有可能与丢失钥匙要做到这一点

解决方案

ASORT($ yourarray,SORT_NUMERIC);的print_r(array_slice($ yourarray,0,3,真实));

www.php/manual/en/function。 asort.php

www.php/manual/en/ function.array-slice.php

So I know that min() can get me the lowest value from an array, however I need the 3 lowest values from an array.

assuming my array is named $myArray:

I used asort($myArray) on my array, then print_r($myArray) outputs this:

Array ( [137] => 4.90416668118 [135] => 7.1203544474 [18] => 7.2476262434 [81] => 8.37903400152 [33] => 9.1074567001 [4] => 9.90788482793 [138] => 10.2493339987 [5] => 11.6024401676 [63]...and so on until [124] => 8727.73285117 [153] => 8727.73285117 [117] => 8727.73285117 )

How can I get the 3 first values or the X first values should I need to...

I SHOULD HAVE SPECIFIED: Is it possible to do this with losing the keys?

解决方案

asort($yourarray, SORT_NUMERIC); print_r(array_slice($yourarray, 0, 3, true));

www.php/manual/en/function.asort.php

www.php/manual/en/function.array-slice.php

更多推荐

从一个数组,PHP得到3个最低值

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

发布评论

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

>www.elefans.com

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