What is the usage of the array_shift function in php?
The array_shift function is used to remove and return the first element of an array. Once removed, the length of the array will decrease by one. If the array is empty, the function will return NULL.
Syntax of the function:
array_shift(array)
Parameter description:
- array: the array that needs to be operated on.
Example:
$array = array(1, 2, 3, 4, 5);
$firstElement = array_shift($array);
print_r($array); // 输出:Array ( [0] => 2 [1] => 3 [2] => 4 [3] => 5 )
echo $firstElement; // 输出:1
In the example above, the first element 1 is removed from the array and assigned to the variable $firstElement. The content of the array is then printed out, showing that the first element has been removed and the length of the array has decreased by one.