开发者

PHP Function to get First 5 Values of array

开发者 https://www.devze.com 2023-02-02 07:33 出处:网络
Array ( [university] => 57 [iit] => 57 [jee] => 44 [application] => 28 [study] => 26 [college] => 23
Array
(
    [university] => 57
    [iit] => 57
    [jee] => 44
    [application] => 28
    [study] => 26
    [college] => 23
    [exam] => 19
    [colleges] => 19
    [vie开发者_StackOverflow中文版w] => 19
    [amp] => 18
)

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


Use array_slice() function:

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


If you need the first 5 elements

  • by order of keys: use ksort(),
  • by order of values: use sort()

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号