30 seconds of PHP

  Back to Home

remove

arraybeginner

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);
}