Function to delete the first and last element of an array in php, php array_PHP tutorial

WBOY
Release: 2016-07-13 10:05:26
Original
736 people have browsed it

Function to delete the first and last element of an array in php, php array

For a php array, how to delete the first or last element of the array What about an element? In fact, both processes can be completed through the functions array_pop and array_shift that come with PHP. Here is a detailed introduction on how to operate.

(1) Use array_pop to delete the last element of the array, for example:

$user=array('apple','banana','orange');
$result=array_pop($user);
print_r($result);
print_r($user);
Copy after login

The result will be:

orange
array('apple','banana')

(2) 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')

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

Copy code The code is as follows:
$user=array_splice($user,1); //Delete the first element of the array. Note that what is returned is the new array after deletion

The following is a brief explanation of array_pop and array_shift:

array_pop() pops and returns the last element of array and decrements 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/963823.htmlTechArticleFunction to delete the first element and the last element of the array in php, php array For a php array, the How to delete the first or last element of the array? Actually this...
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