PHP Function to get First 5 Values of array

前端 未结 2 2085
天命终不由人
天命终不由人 2021-02-06 22:13
Array
(
    [university] => 57
    [iit] => 57
    [jee] => 44
    [application] => 28
    [study] => 26
    [college] => 23
    [exam] => 19
    [c         


        
相关标签:
2条回答
  • 2021-02-06 22:56

    Use array_slice() function:

    $newArray = array_slice($originalArray, 0, 5, true);
    
    0 讨论(0)
  • 2021-02-06 23:00

    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 讨论(0)
提交回复
热议问题