This article mainly introduces the deletion of the first element of the array in PHP and the last element of the function, the need for friends can refer to the following
For a PHP array, how do you delete the first element or the last element of the array? In fact, both of these processes can be done through PHP's own functions Array_pop and Array_shift, the following is a detailed description of how to operate.
(1) Use Array_pop to delete the last element of an array, for example:
$user =array (' Apple ', ' banana ', ' orange '), $result =array_pop ($user);p rint_r ($result);p rint_r ($user);
The result will be:
Orange
Array (' Apple ', ' banana ')
(2) Use Array_shift to delete the first element of an array, for example:
$user =array (' Apple ', ' banana ', ' orange '), $result =array_shift ($user);p rint_r ($result);p rint_r ($user);
The result will be:
Apple
Array (' banana ', ' orange ')
In fact, the first element of the delete array can also use the Array_splice function, namely:
The code is as follows:
$user =array_splice ($user, 1); Delete the first element of the array, and note that the new array is returned when it is deleted
Here's a simple explanation for Array_pop and Array_shift:
Array_pop () pops up and returns the last cell of the array, minus one of the array's length. If array is empty (or not an array), NULL is returned.
Array_shift () Moves the first cell of the array out and returns as a result, reducing the length of the array and moving all other cells forward one bit. All numeric key names are changed from zero to start, and the text key name is unchanged. If the array is empty (or not an array), NULL is returned.