search
Homephp教程php手册PHP函数array_shift()在删除数组元素中的用法介绍

PHP函数array_shift()在删除数组元素中的用法介绍

Jun 13, 2016 am 11:06 AM
arrayphpshiftintroduceelementfunctiondeleteexistbigusarrayarticleusageof

我们在以前的文章中曾经向大家描述过在数组尾端删除数组元素的函数array_pop()的具体用法,今天我们向大家介绍的则是在数组首端删除数组元素的方法。在这里我们将会用到接下来我们就向大家示范PHP函数array_shift()是如何从数组的开头删除元素:

<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>

显示结果如下: 

0 : orange

1 : banana

2 : Peach

3 : pear 

最后,刚才被PHP函数array_shift()删除的元素的值会储存在 $shifted 变量里面,它的值是:apple 。


Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Atom editor mac version download

Atom editor mac version download

The most popular open source editor