Home  >  Article  >  Backend Development  >  How to remove the first element from php array

How to remove the first element from php array

藏色散人
藏色散人Original
2020-07-04 11:28:503355browse

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)

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);
?>

Run result:

redArray ( [0] => green [1] => blue )

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!

Statement:
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