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"

I’m Abhishek, a DevOps, SRE, DevSecOps, and Cloud expert with a passion for sharing knowledge and real-world experiences. I’ve had the opportunity to work with Cotocus and continue to contribute to multiple platforms where I share insights across different domains:
-
DevOps School – Tech blogs and tutorials
-
Holiday Landmark – Travel stories and guides
-
Stocks Mantra – Stock market strategies and tips
-
My Medic Plus – Health and fitness guidance
-
TrueReviewNow – Honest product reviews
-
Wizbrand – SEO and digital tools for businesses
I’m also exploring the fascinating world of Quantum Computing.