search
HomeBackend DevelopmentPHP ProblemHow to delete an element from a php array

How to delete an element from a php array

Jun 12, 2023 pm 05:00 PM
phpphp array

How to delete an element in a php array: 1. The foreach and unset() functions delete specific values ​​in the array; 2. The array_flip() function and unset() function delete specific values ​​in the array; 3. array_search( ) and unset() functions delete specific values ​​in the array; 4. The array_splice() function has the same effect as the unset() function; 5. array_unique() allows all specific values ​​to be deleted from the array; 6. Only specific values ​​are deleted.

How to delete an element from a php array

The operating environment of this tutorial: windows10 system, php8.1.3 version, DELL G3 computer.

php method to delete elements with specified values ​​in the array

In some special cases, you need to delete specific values ​​in the array, and delete them all. In fact, the method There are many types, which we discuss through this article.

1. Use the foreach and unset() functions to delete specific elements in the array

foreach($array as $k=>$v){
if($v == 'day'){
unset($array[$k]):
}
}

The unset() function deletes the specified array value.

2. Use array_flip() function and unset() function to delete specific values ​​in the array

$arr = array_flip($arr);
unset($arr['world']);
$arr = array_flip($arr);
print_r($arr);

array_flip() is an inversion function that changes the original value of the array to The key name becomes the key value, and the key value becomes the key name, so that the above operation is easy to understand.

3. Use the array_search() and unset() functions to delete specific values ​​in the array

if(($key = array_search('day',$arr))){
unset($arr[$key]);
}

array_search() function is the same as in_array(), searching in the array A key value. If the value is found, the key of the matching element is returned. If not found, return false.

4. The array_splice() function can play the same role as the unset() function

if(($key = array_search('day',$arr))){
array_splice($arr, $key,1);
}

array_splice() has four parameters.

5. array_unique() allows this specific value to be left in the array

Note: All the above operations only delete the first one in the array and the one you gave Elements with the same specified value. Even if the subsequent elements are equal to the value you specified, they have not been deleted!

Before performing all the above code operations, first perform the array_unique($array) operation. The duplicate values ​​in the array are merged and deleted, and then the above deletion is performed so that no duplicate values ​​in the array are left.

6. Only delete the key value corresponding to the specific value, leaving other values ​​untouched

In the fifth step, while the specific value is deleted, other irrelevant values ​​are also merged. , then what should we do if we don’t touch other values? Repeat operations one to four above to get the length of the array, and use a for loop. Of course, this is the stupidest way~~

Get the length of the array The function is count($arr)

for(count($arr))if($key = 
array_search('day',$arr))array_splice($arr,$key,1);

The above is the detailed content of How to delete an element from a php array. For more information, please follow other related articles on the PHP Chinese website!

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

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows

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.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.