How to delete the specified value in the array in php

王林
Release: 2023-03-07 11:50:02
Original
2426 people have browsed it

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

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

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

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

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!

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