How to delete values ​​in an array in php

青灯夜游
Release: 2023-03-08 08:28:02
Original
3449 people have browsed it

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.

How to delete values ​​in an array in php

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]):
}
}
Copy after login

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);
Copy after login

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]);
}
Copy after login

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);
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template