PHP array_filter() function
The array_filter()
function in PHP is used to filter elements of an array based on a custom condition. It iterates over each element of the array and passes it to a callback function. If the callback returns true
for an element, that element is included in the resulting array; otherwise, it is removed.
Syntax:
Parameters:
- $array: The input array that will be filtered.
- $callback (optional): A callback function that determines which elements are retained. The callback should return
true
for elements that should be kept andfalse
for those that should be filtered out. If no callback is provided, only elements that are considered "truthy" (non-empty, non-null, non-false) are retained. - $mode (optional): Specifies what arguments are sent to the callback function. It can be one of the following:
ARRAY_FILTER_USE_KEY
(to filter based on keys).ARRAY_FILTER_USE_BOTH
(to filter based on both keys and values).- The default is
0
, which sends only the value to the callback.
Return Value:
- The function returns a filtered array containing only elements that satisfy the condition in the callback function.
Example 1: Basic Usage with Callback
Here's an example where array_filter()
is used to filter out all even numbers from an array:
Output:
In this example, the callback function checks if a value is odd (by checking if it's not divisible by 2). As a result, only odd numbers are retained in the filtered array.
Example 2: Using array_filter()
Without a Callback
When no callback is provided, array_filter()
removes all "falsy" values (i.e., 0
, false
, null
, ''
, etc.).
Output:
Here, array_filter()
removes all falsy values (like 0
, false
, null
, and ''
) from the array, leaving only truthy values.
Example 3: Filtering by Both Key and Value
You can pass both the key and value to the callback function by using the ARRAY_FILTER_USE_BOTH
mode.
Output:
In this case, the callback filters the array based on both keys and values, retaining elements where the key is "a"
or the value is 30
.
Example 4: Filtering by Key Only
You can filter the array based on its keys using ARRAY_FILTER_USE_KEY
.
Output:
Here, array_filter()
filters the array based on keys containing the letter 'a'
.
Practical Usage:
array_filter()
is useful for:- Removing unwanted values from an array (e.g., empty values, zeros, false values).
- Filtering arrays based on custom conditions (e.g., keeping only elements that meet certain criteria).
- Working with associative arrays where both keys and values need to be filtered based on complex logic.
Summary:
array_filter($array, $callback)
filters the input array based on a condition defined in a callback function.- The function returns a new array with only elements that meet the condition.
- When no callback is provided, it removes falsy values from the array.
- The
$mode
parameter allows filtering based on keys or both keys and values.
This function is powerful for refining arrays, making data processing and transformation more flexible.