Home  >  Article  >  Backend Development  >  How to delete the specified value in the array in php

How to delete the specified value in the array in php

王林
王林Original
2020-11-05 17:57:322406browse

php method to delete the specified value in the array: You can delete it through the foreach statement combined with the usnet function, such as [foreach($array as $k=>$v){if($v == 'day'){unset($array[$k])}}].

How to delete the specified value in the array in php

The method is as follows:

(Recommended tutorial: java video tutorial)

1. Utilization The foreach and unset() functions 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);
}

Related recommendations: php training

The above is the detailed content of How to delete the specified value in the 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