Home > Article > Backend Development > How to delete values in an array in php
Method: 1. Use foreach and unset() functions to delete specific values in the array; 2. Use array_flip() and unset() functions to delete specific values in the array; 3. Use array_search() and unset() function to delete a specific value in the array.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php delete array Methods of specifying values
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 the 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, returns false.
[Recommended learning: "PHP Video Tutorial"]
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); }
For more programming-related knowledge, please visit: Programming Video! !
The above is the detailed content of How to delete values in an array in php. For more information, please follow other related articles on the PHP Chinese website!