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

How to remove the first element from php array

藏色散人
藏色散人Original
2021-03-22 10:12:163087browse

How to remove the first element of the php array: 1. Delete the first element of the array through the "array_shift($user);" method; 2. Through the "array_splice($user,1);" method Delete the first element of the array.

How to remove the first element from php array

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer

Use array_shift to delete the first element of the array, for example :

$user=array('apple','banana','orange');
$result=array_shift($user);
print_r($result);
print_r($user);

The result will be:

apple
array('banana','orange')

In fact, you can also use the array_splice function to delete the first element of the array, that is:

 $user=array_splice($user,1); //删除数组第一个元素,注意此时返回的是被删除后的新的数组

【 Recommended learning: PHP video tutorial

The following is a brief explanation of array_pop and array_shift:

array_pop() pops up and returns the last unit of the array array, and Decrease the length of array by one. NULL will be returned if array is empty (or not an array).

array_shift() Shifts the first element of array out and returns it as the result, decrements the length of array by one and shifts all other elements forward one position. All numeric key names will be changed to count from zero, and text key names will remain unchanged. If array is empty (or is not an array), NULL is returned.

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