Removes elements from an array for which the given function returns false
.
Use array_filter()
to find array elements that return truthy values and array_diff_keys()
to remove the elements not contained in $filtered
.
代码实现
function remove($items, $func)
{
$filtered = array_filter($items, $func);
return array_diff_key($items, $filtered);
}
使用样例
remove([1, 2, 3, 4], function ($n) {
return ($n % 2) === 0;
});
// [0 => 1, 2 => 3]