PHP array_shift() Function Guide
The array_shift() function in PHP is used to remove and return the first element of an array. It will re-index the keys of the array and return the value of the removed element.
Here is an example of using the array_shift() function:
$fruits = array("apple", "banana", "orange");
$firstFruit = array_shift($fruits);
echo $firstFruit; // 输出: apple
print_r($fruits); // 输出: Array ( [0] => banana [1] => orange )
In the example above, we defined an array named $fruits and used the array_shift() function to remove the first element of the array. The removed element “apple” is assigned to the variable $firstFruit. Then, we printed the value of the $firstFruit variable, which displayed the removed element “apple”. Next, we used the print_r() function to print the contents of the $fruits array, showing that the first element “apple” has been removed from the array and the array has been re-indexed as [0] => banana and [1] => orange.