This article mainly introduces the function of deleting the first and last elements of an array in php. if you need a php array, refer to the following, how can I delete the first or last element of the array? In fact, these two processes can be completed through the built-in functions array_pop and array_shift in php. The following describes how to operate them.
(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);
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);
The result will be:
Apple
Array ('banana ', 'Orange ')
In fact, you can use the array_splice function to delete the first element of the array, that is:
The code is as follows:
$ User = array_splice ($ user, 1); // delete the first element of the array. Note that the returned element is the new array after deletion.
The following is a brief explanation of array_pop and array_shift:
Array_pop () pops up, returns the last unit of the array, and deletes the length of the array by one. If array is NULL (or not an array), NULL is returned.
Array_shift () removes the first unit of array and returns the result. This removes the length of array and moves all other units one bit forward. The names of all numeric keys are counted from scratch, and the names of text keys remain unchanged. If array is NULL (or not an array), NULL is returned.