php PHP函数获取数组的前5个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4563958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:28:33  来源:igfitidea点击:

PHP Function to get First 5 Values of array

phparraysslice

提问by Mohit

Array
(
    [university] => 57
    [iit] => 57
    [jee] => 44
    [application] => 28
    [study] => 26
    [college] => 23
    [exam] => 19
    [colleges] => 19
    [view] => 19
    [amp] => 18
)

How can I get an array with the first 5 elements?

如何获得包含前 5 个元素的数组?

回答by Crozin

Use array_slice()function:

使用array_slice()功能:

$newArray = array_slice($originalArray, 0, 5, true);

回答by ern0

If you need the first 5 elements

如果您需要前 5 个元素

before the array_slice(). Insert a letter 'r'to the second place for reverse order: krsort(), arsort().

array_slice()之前。将字母'r'插入到倒序的第二个位置:krsort(), arsort()