根据其值从数组中删除元素? [英] remove element from array based on its value?
本文介绍了根据其值从数组中删除元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含键和值的常规数组.
i've got a regular array with keys and values.
是否有一种简单的方法可以根据其值删除数组元素,或者我是否需要foreach循环遍历并检查每个值以删除它?
is there a simple way to remove the array element based on its value or do i have to foreach-loop it through and check every value to remove it?
推荐答案
http://us3.php.net/array_filter
PHP 5.3示例从数组$ a中删除"foo":
PHP 5.3 example to remove "foo" from array $a:
<?php
$a = array("foo", "bar");
$a = array_filter($a, function($v) { return $v != "foo"; });
?>
第二个参数可以是任何类型的PHP回调(例如,函数名称为字符串).如果搜索值不恒定,也可以使用函数生成函数.
The second parameter can be any kind of PHP callback (e.g., name of function as a string). You could also use a function generating function if the search value is not constant.
这篇关于根据其值从数组中删除元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文