In previous articles, we have described to you the specific usage of the function array_pop() to delete array elements at the end of the array. Today we introduce to you the method of deleting array elements at the beginning of the array. . Here we will use Next we will show you how the PHP function array_shift() deletes elements from the beginning of the array:
- <?
- /* 首先我们建立一个数组 */
- $fruitArray = array("apple", "orange", "banana", "Peach", "pear");
- /* 使用 array_shift()函数从数组的开头删除一个元素 */
- $shifted = array_shift($fruitArray);
- /* 现在我们把删除后的数组中所有元素的键(key)与值(value)都显示在网页上 */
- while (list($key,$value) = each($fruitArray)) {
- echo "$key : $value<br>";
- }
- echo "<br>最后,刚才被删除的元素的值会储存在 $shifted 变量里面,它的值是:
- $shifted";
- ?>
The result is as follows:
0 : orange
1 : banana
2 : Peach
3 : pear
Finally, the value of the element just deleted by the PHP function array_shift() It will be stored in the $shifted variable, and its value is: apple.