How to delete an element from php array
PHP array is a frequently used data type. In our daily work, sometimes we need to delete an element or a group of elements from an array. PHP provides a variety of methods to implement this function, and you can choose the appropriate method according to the actual situation.
1. Use the unset() function
The unset() function can be used to delete the element with the specified subscript in the array. The usage method is as follows:
<?php $arr = array('A', 'B', 'C', 'D'); unset($arr[2]); print_r($arr); ?>
The above code will output:
Array ( [0] => A [1] => B [3] => D )
As can be seen from the output results, the element with the specified subscript can be deleted using the unset() function. It should be noted that using the unset() function can only delete elements with a specified subscript, but not elements with a specified value.
2. Use the array_splice() function
array_splice() function can be used to delete a specified element in the array, and can insert new elements after deletion. The usage method is as follows:
<?php $arr = array('A', 'B', 'C', 'D'); array_splice($arr, 2, 1); print_r($arr); ?>
The above code will output:
Array ( [0] => A [1] => B [2] => D )
As can be seen from the output result, the array_splice() function can be used to delete the element with the specified subscript, and the array index will be reordered after deletion. It should be noted that when using the array_splice() function, if there are multiple elements to be deleted, this can be achieved by specifying the third parameter. For example:
<?php $arr = array('A', 'B', 'C', 'D', 'E'); array_splice($arr, 1, 3); print_r($arr); ?>
The above code will output:
Array ( [0] => A [1] => E )
As can be seen from the output result, it is equivalent to deleting the elements with subscripts 1, 2, and 3. It should be noted that when using the array_splice() function to delete elements, you can also insert new elements into the array after deletion, for example:
<?php $arr = array('A', 'B', 'C', 'D', 'E'); array_splice($arr, 1, 3, array('F', 'G')); print_r($arr); ?>
The above code will output:
Array ( [0] => A [1] => F [2] => G [3] => E )
From the output The result can be seen that after deleting the elements with indexes 1, 2, and 3, new elements are inserted into the array.
3. Use array_filter() function
array_filter() function can be used to delete elements that meet specified conditions in the array. The usage method is as follows:
<?php $arr = array(1, 2, 3, 4, 5); $arr = array_filter($arr, function ($item) { return $item % 2 == 0; }); print_r($arr); ?>
The above code will output :
Array ( [1] => 2 [3] => 4 )
As can be seen from the output results, the array_filter() function can be used to filter out the elements in the array that meet the specified conditions, and return the elements that meet the conditions into a new array. It should be noted that when using the array_filter() function to delete elements, the subscripts of the original array will be retained.
In summary, PHP can use the unset() function, array_splice() function and array_filter() function to delete elements in an array. You can choose the appropriate method according to the actual situation.
The above is the detailed content of How to delete an element from php array. 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



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.

PHP 8's JIT compilation enhances performance by compiling frequently executed code into machine code, benefiting applications with heavy computations and reducing execution times.

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.

The article discusses symmetric and asymmetric encryption in PHP, comparing their suitability, performance, and security differences. Symmetric encryption is faster and suited for bulk data, while asymmetric is used for secure key exchange.

The article discusses implementing robust authentication and authorization in PHP to prevent unauthorized access, detailing best practices and recommending security-enhancing tools.

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

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 to prevent CSRF attacks in PHP, including using CSRF tokens, Same-Site cookies, and proper session management.
