且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-08-27 19:35:58

  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