How to remove specific elements from an array in php
Method: 1. Use unset() to remove the element with the specified key name, the syntax is "unset($array name[key name])"; 2. Use array_splice() to delete multiple elements starting at the specified position, Syntax "array_splice(array, starting position, number of elements)"; 3. Use array_shift() to delete the first element, the syntax "array_shift(array)"; 4. Use array_pop() to delete the last element, the syntax "array_pop( array)".
The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer
Method 1: Use The "$array name[key name]" statement and the unset() function remove an element of the specified key name
$array name[key name]
The statement can access an element of the specified key nameand the unset() function can delete an array element of the specified subscript (key name), that is, delete the accessed element
<?php header('content-type:text/html;charset=utf-8'); $arr = array("Name"=>"Peter","Age"=>"41","Country"=>"USA"); echo "原数组:"; var_dump($arr); echo "删除指定键名的一个元素后:" ; unset($arr["Age"]); //↑ 你想删除的key(下标) var_dump($arr); ?>
Method 2: Use the array_splice() function to delete one or more elements starting at the specified position
array_splice() is a powerful function with multiple functions: it can insert array elements, replace array elements, and of course, it can also delete array elements (after all, the job of the array_splice() function is to delete specified elements. and replaced with other values).
The array_splice() function can delete the specified number of elements starting from the specified position.
array_splice($array,$start,$length)
Parameters | Description |
---|---|
array | Required . Specifies an array. |
start | Required. numerical value. Specifies the starting position of deleted elements.
0 = first element.
If the value is set to a positive number, removal begins at the offset specified by the value in the array. If the value is set to a negative number, removal begins at the offset specified by the value from the end of the array.
-2 means start from the second to last element of the array. |
length | Optional. numerical value. Specifies the number of elements to be removed, which is also the length of the returned array.
If this value is set to a positive number, remove this number of elements. If this value is set to a negative number, all elements from start to length inverse of the end of the array are removed. If this value is not set, all elements from the position set by the start parameter to the end of the array are removed. |
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相配合,进行插入操作(这里就不做具体介绍了)。
方法3:使用array_shift()函数删除第一个元素
array_shift() 函数用于删除数组中的第一个元素,并返回被删除的元素。
array_shift($arr)
array_shift()函数在删除$arr数组的开头的第一个元素后,arr 数组的长度会减 1,并将所有其他元素向前移动一位。如果键名是数字的,所有元素都将获得新的键名,从 0 开始,并以 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); ?>
可以看到这个例子,我们原先的$arr数组里面有5个元素,使用array_shift($arr)方法之后,重新使用var_dump($arr)输出数组,发现只有4个元素了,数组头部元素被删除了。
方法4:使用array_pop() 函数删除最后一个元素
array_pop()函数删除数组中的最后一个元素。
array_pop($array)
该函数会改变原数组;返回值为数组的最后一个值,如果数组是空的,或者不是一个数组,将返回 NULL。
<?php header('content-type:text/html;charset=utf-8'); $arr = array(1,2,3,4,5,6,7,8,9,10); echo "原数组:"; var_dump($arr); array_pop($arr); echo "移除最后一项后:"; var_dump($arr); ?>
注:利用array_slice()函数也可实现删除第一个元素和最后一个元素。
推荐学习:《PHP视频教程》
The above is the detailed content of How to remove specific elements from an array in php. For more information, please follow other related articles on the PHP Chinese website!

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

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
