Here, I will show you the php array to get the first 5 elements. let’s discuss about php array getting 5 first element values. you can see how to get 5 first element of the array in PHP. This post will give you a simple example of PHP getting 5 first key of an array example.
we will use the array_slice() function to get the first 5 elements of an array in PHP. so, let’s see the simple code of how to get the first 5 values in a php array.
Example:
<?php
$myArray = ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight"];
$firstElems = array_slice($myArray, 0, 5);
var_dump($firstElems);
?>