In PHP, deleting an array element is not a difficult thing. You only need to use the array_shift() function to delete the first array element and return the deleted element.
The syntax of the array_shift() function is as follows:
mixed array_shift ( array &$array )
This function receives an array as a parameter and returns the value of the first element deleted in the array. If the array is empty, NULL is returned.
The following is a sample program to delete the first array element:
<?php $languages = array("PHP", "JavaScript", "Java", "C#", "Ruby"); $firstLanguage = array_shift($languages); echo "删除的第一个语言是:" . $firstLanguage . "<br>"; echo "新的数组内容是:<br>"; print_r($languages); ?>
The running result is:
删除的第一个语言是:PHP 新的数组内容是: Array ( [0] => JavaScript [1] => Java [2] => C# [3] => Ruby )
As can be seen from the above code:
It should be reminded that when using the array_shift() function to delete the first element of the array, you need to pay attention to the following points:
In short, using the array_shift() function can easily delete the first element of an array, making our PHP code simpler and more efficient.
The above is the detailed content of How to delete the first element of an array in php. For more information, please follow other related articles on the PHP Chinese website!