Home > Backend Development > PHP Tutorial > How to get the first element of an array in php

How to get the first element of an array in php

藏色散人
Release: 2023-04-04 14:10:02
Original
15266 people have browsed it

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.

How to get the first element of an array in php

# 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 => &#39;white&#39;, 6 => &#39;green&#39;, 11=> &#39;red&#39;);
echo reset($color);
Copy after login

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:

How to get the first element of an array in php

#The reset function means to point the internal pointer of the array to the first unit.

reset syntax:

mixed reset ( array &$array )
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template