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:
<ol class="dp-xml"> <li class="alt"><span><span><? </span></span></li> <li class=""><span> </span></li> <li class="alt"><span>/* 首先我们建立一个数组 */ </span></li> <li class=""><span> </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">fruitArray</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array</font></span><span>("apple", "orange", "banana", "Peach", "pear"); </span> </li> <li class=""><span> </span></li> <li class="alt"><span>/* 使用 array_shift()函数从数组的开头删除一个元素 */ </span></li> <li class=""><span> </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">shifted</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array_shift</font></span><span>($fruitArray); </span> </li> <li class=""><span> </span></li> <li class="alt"><span>/* 现在我们把删除后的数组中所有元素的键(key)与值(value)都显示在网页上 */ </span></li> <li class=""><span> </span></li> <li class="alt"><span>while (list($key,$value) = each($fruitArray)) { </span></li> <li class=""><span> </span></li> <li class="alt"><span>echo "$key : $value<br>"; </span></li> <li class=""><span> </span></li> <li class="alt"><span>} </span></li> <li class=""><span> </span></li> <li class="alt"><span>echo "<br>最后,刚才被删除的元素的值会储存在 $shifted 变量里面,它的值是: </span></li> <li class=""><span> </span></li> <li class="alt"><span>$shifted"; </span></li> <li class=""><span> </span></li> <li class="alt"><span>?> </span></li> </ol>
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.