Home >Backend Development >PHP Tutorial >Analyze the specific usage of PHP function array_pop()_PHP tutorial
We once introduced to you how to add elements to the head and tail of an array. Below we will introduce to you the specific method of how to delete array elements at the end of the array. First, the function we need to use is the PHP function array_pop().
<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_pop()函数从数组的尾端删除一个元素 */ </span></li> <li class=""><span> </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">popped</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array_pop</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>最后,刚才被删除的元素的值会储存在 $popped 变量里面,它的值是:$popped"; </span></li> <li class=""><span> </span></li> <li class="alt"><span>?> </span></li> </ol>
The result is as follows:
0 : apple
1 : orange
2 : banana
3 : Peach
Finally, the value of the element deleted by the PHP function array_pop() will be stored in the $popped variable , its value is: pear.