Introduction to the usage of PHP function array_shift() in deleting array elements_PHP tutorial

WBOY
Release: 2016-07-15 13:29:11
Original
1062 people have browsed it

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>
Copy after login

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.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446377.htmlTechArticleIn our previous article, we have described to you the function array_pop() that deletes array elements at the end of the array. The specific usage, what we introduce to you today is to delete the number at the beginning of the array...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!