ajsie ajsie - 2 years ago 66
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 Source


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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download