How to Get the First 5 Elements of Array in PHP?

Posted by

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);
     
?>

Output:

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x