从一个数组,PHP得到3个最低值 [英] Get 3 lowest values from an array, PHP

查看:109
本文介绍了从一个数组,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,真实));

http://www.php.net/manual/en/function。 asort.php

http://www.php.net/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));

http://www.php.net/manual/en/function.asort.php

http://www.php.net/manual/en/function.array-slice.php

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

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