search
Homephp教程php手册php删除数组元素示例分享

这篇文章主要介绍了php删除数组元素示例,需要的朋友可以参考下

PHP删除数组元素的具体方法:

1.用unset()方法:

复制代码 代码如下:


$a=array("red", "green", "blue", "yellow");  
count($a); //得到4  
unset($a[1]); //删除第二个元素  
count($a); //得到3  
echo $a[2]; //数组中仅有三个元素,本想得到最后一个元素,但却得到blue,  
echo $a[1]; //无值  
?>

缺点:删除数组中的元素后,数组中的元素个数(用count()得到)变了,但数组下标却没有重新排列,还必须用PHP删除数组元素前的key来操作相应的值.

2.用array_splice()方法:

复制代码 代码如下:


$a=array("red", "green", "blue", "yellow");  
count ($a); //得到4  
array_splice($a,1,1); //删除第二个元素  
count ($a); //得到3  
echo $a[2]; //得到yellow  
echo $a[1]; //得到blue
?>

这个程序和前一个相对比,就可以看到,array_splice()不仅删除了元素,还把元素重排了,这样在数组各元素中间就不会有空值!

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

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

Atom editor mac version download

Atom editor mac version download

The most popular open source editor