array_filter
Filters elements of an array using a callback function, returning only elements that pass the test.
Parameters
main.php
Return Values
array
Returns the filtered array with preserved keys
On success:Array containing only elements that passed the callback test
On failure:Empty array if no elements pass the test
Notes
- Preserves original array keys unless reindexed with array_values()
- Without callback, removes all falsy values (empty, null, false, 0)
- Use ARRAY_FILTER_USE_KEY to filter by keys instead of values
- Use ARRAY_FILTER_USE_BOTH to pass both key and value to callback
Changelog
- 8.0.0: callback is now nullable
- 5.6.0: Added mode parameter
Related Functions
array_push
ArrayPushes one or more elements onto the end of an array and returns the new number of elements.
arrayaddqueue
array_map
ArrayApplies a callback function to the elements of one or more arrays and returns a new array with the results.
transformcallbackmap
array_unique
ArrayRemoves duplicate values from an array and returns the array with unique values only.
uniquededuplicatefilter
array_diff
ArrayCompares two or more arrays and returns the values from the first array that are not present in any of the other arrays.
comparedifferencefilter