Home > Article > Backend Development > How to use array_shift function to delete the first element of an array in PHP
In PHP, array is a very useful data type, which can be used to store a series of data. When we need to delete elements in an array, we can use the array_shift function provided in PHP to delete the first element in the array. This article will detail how to use the array_shift function to delete the first element of an array.
Definition of array_shift function
The array_shift function is a function in PHP that deletes the first element in an array and returns the deleted element. The function prototype is as follows:
mixed array_shift (array &$array)
Among them, the $array parameter is a reference to the array to be operated on, and the return value is the first element to be deleted. value, or NULL if the array is empty.
Steps to use the array_shift function to delete the first element of an array
The following are the steps to use the array_shift function to delete the first element of an array:
Step 1: Define an array
First, you need to define an array in order to delete it. Suppose we have defined an array $fruits as follows:
$fruits = array("apple", "banana", "cherry", "date");
Step 2: Use the array_shift function to delete the first element of the array
Next, you need to use the array_shift function to delete the first element in the $fruits array. After deletion, the elements in the array $fruits will be reordered so that the element originally ranked second becomes the new first element. The code for the deletion operation is as follows:
$first_fruit = array_shift($fruits);
Description: The array_shift function will return the first deleted element and reorder the remaining elements. . In the above code, the $first_fruit variable will hold the value of the first deleted element.
Step 3: Print the first deleted element and the remaining elements
Finally, you need to print out the first deleted element and the remaining elements. The code is as follows:
echo "The first fruit was: " . $first_fruit . "
";
echo "The remaining fruits are: " . implode(", ", $fruits) ;
Note: The above code will first print out the value of the first deleted element, and then print out the remaining elements. Use the implode function to convert the remaining elements into strings, separated by commas and spaces.
Full code example
The following is a complete code example using the array_shift function to delete the first element of an array:
$fruits = array(" apple", "banana", "cherry", "date");
$first_fruit = array_shift($fruits);
echo "The first fruit was: " . $first_fruit . "
";
echo "The remaining fruits are: " . implode(", ", $fruits);
?>
Output result:
The first fruit was: apple
The remaining fruits are: banana, cherry, date
Conclusion
Using the array_shift function can conveniently delete the first element in the array and return the value of the deleted element . In addition to the array_shift function, PHP also provides other array operation functions, such as array_pop, array_push, array_unshift, etc. In actual development, choosing the appropriate function based on actual needs can help us manage arrays more efficiently.
The above is the detailed content of How to use array_shift function to delete the first element of an array in PHP. For more information, please follow other related articles on the PHP Chinese website!