How to remove the first element from php array

藏色散人
Release: 2023-03-08 21:28:02
Original
3106 people have browsed it

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);
Copy after login

The result will be:

apple
array('banana','orange')
Copy after login

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); //删除数组第一个元素,注意此时返回的是被删除后的新的数组
Copy after login

【 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!

Related labels:
php
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