Home > Backend Development > PHP Tutorial > 取出数组中,前三小的三个单元,非连续索引数组

取出数组中,前三小的三个单元,非连续索引数组

WBOY
Release: 2016-06-23 13:15:58
Original
817 people have browsed it

$arr =array(	11=>2530,	13=>8922,	26=>250,	77=>250,	88=>1230,	99=>12);
Copy after login

以上数组,怎么样才能取出最小的三项:
array(99=>12,26=>250,77=>250)
Copy after login


回复讨论(解决方案)

$arr =array(    11=>2530,    13=>8922,    26=>250,    77=>250,    88=>1230,    99=>12);asort($arr);print_r(array_slice($arr, 0, 3, true));
Copy after login
Copy after login
Array(    [99] => 12    [77] => 250    [26] => 250)
Copy after login
Copy after login

$arr =array(    11=>2530,    13=>8922,    26=>250,    77=>250,    88=>1230,    99=>12);asort($arr);print_r(array_slice($arr, 0, 3, true));
Copy after login
Copy after login
Array(    [99] => 12    [77] => 250    [26] => 250)
Copy after login
Copy after login


我真是糊涂了,竟然忘了asort和array_slice,谢谢版主大人,又一次帮了我~~~
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template