Get First Element of Array

Posted by

This example will teach you how to retrieve the first element of an array in PHP. I simply walked you through the process of getting the first element of an array in PHP. I simply provided an example of how to use php to fetch the first key of an array. I would like to demonstrate how to retrieve the first key value from an array in PHP.

To retrieve the first element of an array in PHP, we will utilize the array_shift() and array_values() functions. So let us look at the short code for it.

Eaxmple

<?php
  
   $myArray = ["One", "Two", "Three", "Four"];
      
   $firstElem = array_shift(array_values($myArray));   
     
   var_dump($firstElem);
     
?>

OutPut:

string(3) "One"
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