Home  >  Article  >  Backend Development  >  PHP implementation method to remove array elements according to specified element value_PHP tutorial

PHP implementation method to remove array elements according to specified element value_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:23:441022browse

Remove array elements by specified element value

Copy code The code is as follows:

//Remove value The element of "Cat"
$a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
print_r( $a);
unset($a[array_search("Cat",$a)]);//array_search("Cat",$a) returns the key name by element value. Keep index after removal
print_r($a);
?>

View array_search usage

Display results

Before removal:
Array
(
[a] => Dog
[b] => Cat
[c] => Horse
)

After removal:

Array
(
[a] => Dog
[c] => Horse
)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324435.htmlTechArticleRemove array elements according to the specified element value. Copy the code. The code is as follows: ?php //Remove elements with the value "Cat" $a=array("a"="Dog","b"="Cat","c"="Horse"); print_r($a); unset($a[array_search("Cat"...
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