Home > Article > Backend Development > Detailed explanation of php array_shift() function: delete the first element in the array
What is the function of php array_shift() function?
The array_shift() function in php deletes the first element in the array and returns the value of the deleted element. This article introduces the usage and definition of the php array_shift() function in detail. If you are interested, you can take a look. I hope it will be helpful to everyone.
Definition and usage
array_shift() function deletes the first element in the array and returns the value of the deleted element, or NULL if the array is empty. . If the key name is numeric, all elements will get new key names, starting from 0 and incrementing by 1 (refer to the code in Example 2 below), and the literal key values will remain unchanged.
Syntax
array_shift(array)
The parameter array is a specified array and must be specified.
Example 1
Use the php array_shift() function to delete the first element in the $stack array and return the deleted element. The code is as follows
<?php $stack = array("orange", "banana", "apple", "raspberry"); $fruit = array_shift($stack); print_r($stack); ?>
The code execution output is:
After the above code deletes the first element in the $stack array, The element orange is assigned to $fruit.
Example 2
Use the php array_shift() function to delete the array with numeric key names. The code is as follows:
<?php $a=array(0=>"red",1=>"green",2=>"blue"); echo array_shift($a); print_r ($a); ?>
The code running output results:
You can see that the key name of the original element "green" is "1", but after using the array_shift() function, all key names will start from 0
【Recommended related articles】
Definition and usage of php array function array_push()
array_push in php (), array_pop() and array_shift() function usage examples
The above is the detailed content of Detailed explanation of php array_shift() function: delete the first element in the array. For more information, please follow other related articles on the PHP Chinese website!