How to remove the first element from php array

藏色散人
Release: 2023-03-01 22:20:02
Original
3416 people have browsed it

In PHP, you can use the "array_shift()" function to remove the first element in the array. The syntax of this function is "array_shift(array)". Its parameter "array" represents the array, and the return value is from the array. The value of the deleted element.

How to remove the first element from php array

php array removes the first element

Definition and usage

array_shift() function Removes the first element from the array and returns the value of the removed element.

Note: If the key is numeric, all elements will get new keys, starting at 0 and increasing by 1 (see example below).

Syntax

array_shift(array)
Copy after login

Parameters

array required. Specifies an array.

Return value: Returns the value of the element removed from the array, or NULL if the array is empty.

Use numeric key name:

<?php
$a=array(0=>"red",1=>"green",2=>"blue");
echo array_shift($a);
print_r ($a);
?>
Copy after login

Run result:

redArray ( [0] => green [1] => blue )
Copy after login

For more related knowledge, please visit PHP Chinese website!

The above is the detailed content of How to remove the first element from php array. 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