Delete function: 1. array_shift(), which can delete the first element; 2. array_pop(), which can delete the last element; 3. array_unique(), which can remove duplicate elements in the array; 4. array_splice(), you can delete the specified number of elements starting from the specified position, the syntax is "array_splice(array, position, number)"; 5. unset(), you can delete an array element at the specified subscript, the syntax is "unset(array name) [subscript])".
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer
There are multiple array elements built into PHP that can be deleted function, let’s take a look below.
1. array_shift() function
array_shift() function is used to delete the first element in the array and return the deleted element.
Note: If the key name is numeric, all elements will get new key names, starting from 0 and increasing by 1
<?php header("Content-type:text/html;charset=utf-8"); $arr=array(10,12,20,25,24); echo "原数组:"; var_dump($arr); array_shift($arr); echo "删除后的数组:" ; var_dump($arr); ?>
2. array_pop() function
array_pop() function deletes the last element in the array.
<?php header("Content-type:text/html;charset=utf-8"); $arr=array(10,12,20,25,24); echo "原数组:"; var_dump($arr); array_pop($arr); echo "删除后的数组:" ; var_dump($arr); ?>
3. array_unique() function
array_unique() function is used to remove duplicate values in the array. If two or more array values are the same, only the first value is retained and the other values are removed.
Note: The retained array will retain the key type of the first array item.
array_unique(array,sortingtype)
Parameters | Description |
---|---|
array | Required . Specifies an array. |
sortingtype | Optional. Specifies the sorting type. Possible values:
|
<?php header("Content-type:text/html;charset=utf-8"); $arr=array(1,2,3,4,4,2,6); echo "原数组:"; var_dump($arr); echo "删除重复值后:" ; var_dump(array_unique($arr)); ?>
4、array_splice()函数
array_splice()是一个强大的函数,有多重功能:可以插入数组元素、可以替换数组元素,当然也可以删除数组元素(毕竟array_splice()函数的本职工作是删除指定元素并用其它值取代)。
array_splice()函数可以从指定位置开始删除指定个数的元素。
array_splice($arr,$start)
会删除从$start位置开始的所有元素删除。
<?php header("Content-type:text/html;charset=utf-8"); $arr=array(10,12,20,25,24); echo "原数组:"; var_dump($arr); echo "删除后的数组:" ; array_splice($arr,2); var_dump($arr); ?>
$start参数有三种取值情况:
为正数,那么从$start位置开始,往后删除;
为0,那么从第一个元素开始,往后删除;
为负数,则从距离 $arr 末端 -start 的位置开始,从后往前删除。例如 -2 意味着从数组的倒数第二个元素开始。
<?php header("Content-type:text/html;charset=utf-8"); $arr=array(10,12,20,25,24); echo "原数组:"; var_dump($arr); echo "删除后的数组:" ; array_splice($arr,-2); var_dump($arr); ?>
array_splice()函数是强大的,可以删除多个元素,也可只删除一个元素,那就需要给该函数指定一个$length参数(第三个参数),该参数用来规定删除的元素个数。
<?php header("Content-type:text/html;charset=utf-8"); $arr=array(10,12,20,25,24); echo "原数组:"; var_dump($arr); echo "删除后的数组:" ; array_splice($arr,2,1); var_dump($arr); ?>
$length参数也有三种取值情况:
为正数,那么就表示删除 length 个元素;
为负数,那么将删除从 start 开始,到数组末尾倒数 length 为止的所有元素;
如果省略,那么将删除从 start 开始,一直到数组末尾的所有元素。
<?php header("Content-type:text/html;charset=utf-8"); $arr=array(10,12,20,25,24); echo "原数组:"; var_dump($arr); echo "删除后的数组:" ; array_splice($arr,2,-1); var_dump($arr); ?>
$length参数还可以为0,那么就表示不删除元素,可以和该函数的第四个参数$value相配合,进行插入操作(这里就不做具体介绍了)。
5、unset()函数
unset()函数可以删除指定下标(键名)的一个数组元素
<?php header('content-type:text/html;charset=utf-8'); $arr = array(0 => "a", 1 => "b", 2 => "c"); echo "原数组:"; var_dump($arr); echo "删除后的数组:" ; unset($arr[1]); //↑ 你想删除的key var_dump($arr); ?>
推荐学习:《PHP视频教程》
The above is the detailed content of What are the functions in php that can delete array elements?. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor