PHP Function to get First 5 Values of array

2019-03-22 16:11发布

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?

标签: php arrays slice
2条回答
贼婆χ
2楼-- · 2019-03-22 16:35

If you need the first 5 elements

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

查看更多
不美不萌又怎样
3楼-- · 2019-03-22 16:52

Use array_slice() function:

$newArray = array_slice($originalArray, 0, 5, true);
查看更多
登录 后发表回答