ajsie ajsie - 5 months ago 8x
PHP Question

remove element from array based on its value?

i've got a regular array with keys and values.

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?



PHP 5.3 example to remove "foo" from array $a:

$a = array("foo", "bar");
$a = array_filter($a, function($v) { return $v != "foo"; });

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.