Mohit
Mohit

Reputation: 1234

PHP Function to get First 5 Values of array

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?

Upvotes: 49

Views: 61582

Answers (2)

ern0
ern0

Reputation: 3172

If you need the first 5 elements

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

Upvotes: 5

Crozin
Crozin

Reputation: 44396

Use array_slice() function:

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

Upvotes: 120

Related Questions