Home  >  Article  >  Backend Development  >  How to delete an element from an array in php

How to delete an element from an array in php

藏色散人
藏色散人Original
2020-07-27 10:21:165729browse

php method to delete an element of an array: 1. Use the foreach and unset functions to delete specific elements in the array; 2. Use the array_flip function and unset function to delete specific values ​​in the array; 3. Use array_search and unset Function deletes a specific value from an array.

How to delete an element from an array in php

Recommended: "PHP Tutorial"

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

In some special cases, you need to delete specific values ​​in the array, and delete them all. In fact, there are many methods, we will discuss them 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 a reversal function that changes the original key name of the array into a key value , change the key value into the key name, so that the above operation is easy to understand.

3. Use 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 for a key value in the array. If the value is found, the key of the matching element is returned. If not found, returns 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 alone in the array

Note: All the above operations only delete the first element in the array that is the same as the value you gave. , even if the subsequent elements are equal to the value you gave, they have not been deleted!

Before performing all the above code operations, first perform the array_unique($array) operation to merge and delete the duplicate values ​​in the array, and then perform the above deletion so that no duplicate values ​​in the array are left. .

6. Only delete the key value corresponding to the specific value and leave other values ​​untouched

In the fifth step, while deleting the specific value, other irrelevant values ​​are also merged, so do not touch other values. what to do? Just repeat the operations one to four above. To get the array length, use a for loop. Of course, this is the stupidest way~~

The function to get the array length 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 an array in php. 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