Is it possible to get the first element of an array in php?

青灯夜游
Release: 2023-03-16 14:12:02
Original
2564 people have browsed it

php can get the first element of the array. PHP provides multiple acquisition methods: 1. Use array_shift() to delete the first element of the array, and return the deleted element, the syntax is "array_shift(array)"; 2. Use array_slice() to intercept the array, and return the first element containing the array. subarray, the syntax is "array_slice(array,0,1)"; 3. Use current() to return the current element, and initially return the first element, the syntax is "current(array)".

Is it possible to get the first element of an array in php?

The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer

php can get the first one in the array element.

php There are many ways to get the first element of the array:

Method 1: Use the array_shift() function

array_shift() function is used to delete the first element in the array and return the deleted first element.

<?php
header("Content-type:text/html;charset=utf-8");
$arr=array(10,12,20,25,24);
echo "原数组:";
var_dump($arr);
$res=array_shift($arr);

echo "数组第一个元素为:".$res ;
?>
Copy after login

Is it possible to get the first element of an array in php?

Method 2: Use the array_slice() function

array_slice() function is used to intercept the array, that is Extract a fragment from the array

This function can intercept the specified number of array elements starting from the specified position.

Return value: Return the intercepted subarray.

<?php
header("Content-type:text/html;charset=utf-8");
$arr=array(10,12,20,25,24);
echo "原数组:";
var_dump($arr);
$res=array_slice($arr,0,1);

echo "数组第一个元素为:" ;
var_dump($res);
?>
Copy after login

Is it possible to get the first element of an array in php?

Method 3: Use the current() function

current() can return the value of the current element of the array, initially Points to the first element.

<?php
header("Content-type:text/html;charset=utf-8");
$arr=array(10,12,20,25,24);
echo "原数组:";
var_dump($arr);
echo "数组第一个元素为:".current($arr) ;
?>
Copy after login

Is it possible to get the first element of an array in php?

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of Is it possible 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