How to delete the head and tail of a php array: 1. Move the unit at the beginning of the array out of the array through "array_shift"; 2. Pop the last unit of the array through "array_pop".
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
Delete the beginning unit elements
array_shift
array_shift — Move the first element of the array out of the array
Description
mixed array_shift ( array &$array )
array_shift() Move the first element of the array out of the array cells are removed and returned as the result, decrementing the length of the array by one and moving all other cells forward by one. All numeric key names will be changed to count from zero, and text key names will remain unchanged. Note: Using this function will reset (reset()) the array pointer.
Parameter array
Input array.
Return value
Returns the removed value, or NULL if array is empty or not an array.
Example
Example #1 array_shift() 例子 $stack = array("orange", "banana", "apple", "raspberry"); $fruit = array_shift($stack); print_r($stack); ?>
The above routine will output: Array( [0] => banana [1] => apple [2] => raspberry)
And orange is assigned to $fruit.
Recommended study: "PHP Video Tutorial"
Delete the tail unit element
array_pop
array_pop — Pop the last unit of the array (pop off the stack)
Description
mixed array_pop ( array &$array )
array_pop() Pops and returns the last unit of the array array, and changes the length of the array array minus one. NULL will be returned if array is empty (or not an array). In addition, a Warning will be generated if the called value is not a number. Note: Using this function will reset (reset()) the array pointer.
Parameter array
Needs to make a stack array.
Return value
Returns the last value of array. If array is empty (if it is not an array), NULL will be returned.
Example
Example #1 array_pop() Example
$stack = array("orange", "banana", "apple", "raspberry"); $fruit = array_pop($stack); print_r($stack); ?>
After this operation, $stack will only have 3 units: Array( [0] => orange [ 1] => banana [2] => apple)
and rasberry will be assigned to $fruit.
The above is the detailed content of How to delete the head and tail of a php array. For more information, please follow other related articles on the PHP Chinese website!