ajsie ajsie - 7 months ago 11
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?

Answer

http://us3.php.net/array_filter

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

<?php
$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.

Comments