Home >Backend Development >PHP Problem >How to delete an element from a php array

How to delete an element from a php array

zbt
zbtOriginal
2023-06-12 17:00:183010browse

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