How to Get the First Element of an Array in PHP?

Problem:

You want to know the value of the first element of an array. 

 

Solution:

You can use built-in reset() function to know the first element of an array. This function sets the internal pointer of the array to the first element and returns it.  See the following example-

[php]
<?php
$months = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
$first_month = reset($months);
echo $first_month;
?>
[/php]

[wpdm_file id=78]

Output:
January

Leave a Reply

Your email address will not be published. Required fields are marked *