PHP

How to get the first element of an array using PHP

Written by Ian Carnaghan · 13 sec read >

If you have the array

$array = array('a','b','c','d');

you will need to get the value a from the array

Answer:

Use the array_shift function available in PHP 4 and 5 which shifts an element off the beginning of an array. See example below

$array = array('a','b','c','d');
$first_element = array_shift($array);
echo $first_element;

The above code will output a

Last Updated On March 22, 2018
Written by Ian Carnaghan
I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment. Profile

Leave a Reply

avatar
  Subscribe  
Notify of