The php array_filter() function gives us the ability to filter an array with a callback function. You can filter both values and keys with array_filter()
function greater_than_5($var) {
return $var > 5;
}
$array = array(1,2,3,4,5,6,7,8,9,10);
print_r(array_filter($array, "greater_than_5"));
// Output:
Array
(
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
)
When working with collections of data in php, the ability to add to, remove from, and manipulate those collections is valuable.
One such operation is filtering an array and removing elements based on conditions.
The php array_filter() function gives us the ability to filter arrays based on callback functions. The callback function should check each array element with an expression and return true or false.
With array_filter(), we can remove elements from our arrays in php with logical expressions.
Below is a simple example showing how you can use array_filter() to filter out all elements of an array where the elements are not greater than 5.
function greater_than_5($var) {
return $var > 5;
}
$array = array(1,2,3,4,5,6,7,8,9,10);
print_r(array_filter($array, "greater_than_5"));
// Output:
Array
(
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
)
Your callback function can be any function which returns a boolean value. You can also define and pass the callback function directly to array_filter().
Below is another example of how you can define and pass a callback function to array_filter().
$array = array("string","hi","berries","soup");
print_r(array_filter($array, function($var) {
return strlen($var) > 5;
}));
// Output:
Array
(
[0] => string
[2] => berries
)
Removing All Empty Elements from a Given Array with php array_filter() Function
If you don’t pass a callback function, then array_filter() removes all empty elements from the given array.
Empty elements include 0, null, false, empty strings, etc.
Below is an example in php of how to use array_filter() to remove all empty elements by not passing a callback function.
$array = array(0, null, false, 1, '', '0', "pink", "bear");
print_r(array_filter($array));
// Output:
Array
(
[3] => 1
[6] => pink
[7] => bear
)
Filtering an Array by Key with php array_filter() Function
You can also filter an array by its keys with array_filter(). There is an optional parameter called ‘mode’ that allows us to filter the array by its keys or by both its keys and values.
Passing ‘ARRAY_FILTER_USE_KEY’ will apply the callback function to an array’s keys. ‘ARRAY_FILTER_USE_BOTH’ applies the callback function to both an array’s values and keys.
Below is an example of how you can filter an array in php by the keys of the array with the php array_filter() function.
$array = array("word" => "string", "greeting" => "hi", "fruit" => "berries", "dinner" => "soup");
print_r(array_filter($array, function($k) {
return strlen($k) > 5;
}, ARRAY_FILTER_USE_KEY));
// Output:
Array
(
[greeting] => hi
[dinner] => soup
)
As you can see, we only removed the elements where the keys didn’t have a string length of at least 5.
Below is another example of how you can filter an array by applying conditions to both the keys and values of the array.
$array = array("word" => "string", "greeting" => "hi", "fruit" => "berries", "dinner" => "soup");
print_r(array_filter($array, function($v, $k) {
return strlen($k) > 5 || $v == "string";
}, ARRAY_FILTER_USE_BOTH));
// Output:
Array
(
[word] => string
[greeting] => hi
[dinner] => soup
)
Hopefully this article has been useful for you to learn how to use the php array_filter() function to filter the elements from an array with a callback function.