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