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?
The array_keys() function returns an array containing the keys.
There are several methods to get the first element of an array in PHP. Some of the methods are using foreach loop, reset function, array_slice function, array_values, array_reverse, and many more.
The array_flip() function flips/exchanges all keys with their associated values in an array.
The array_splice() function removes selected elements from an array and replaces it with new elements. The function also returns an array with the removed elements. Tip: If the function does not remove any elements (length=0), the replaced array will be inserted from the position of the start parameter (See Example 2).
Use array_slice()
function:
$newArray = array_slice($originalArray, 0, 5, true);
If you need the first 5 elements
before the array_slice(). Insert a letter 'r' to the second place for reverse order: krsort(), arsort().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With