Home >Backend Development >PHP Tutorial >How to get the first element of an array in php
php gets the first element of the array, we can do it through the reset function. So in our daily project development process, PHP sometimes needs to use it to obtain the first element of the array. For PHP newbies, it may not be clear yet.
# Below we will introduce to you how to get the first element of an array in PHP through a simple code example.
The specific code example is as follows:
<?php $color = array(4 => 'white', 6 => 'green', 11=> 'red'); echo reset($color);
There is an array $color. We can intuitively see that the first element value of this array is white.
So now if we want to output the first element, we can use the reset function.
The output result is as follows:
#The reset function means to point the internal pointer of the array to the first unit.
reset syntax:
mixed reset ( array &$array )
reset() Rewinds the internal pointer of array to the first unit and returns the value of the first array unit.
The parameter array represents the input array. The return value is the value of the first element of the array, or FALSE if the array is empty.
This article is an introduction to the method of obtaining the first element of an array in PHP. It is very simple and easy to understand. I hope it will be helpful to friends in need!
The above is the detailed content of How to get the first element of an array in php. For more information, please follow other related articles on the PHP Chinese website!