PHP has a vast collection of functions that make developers’ lives easier by reducing the complexity of their code. One such function is array_filter()
. It is a built-in function that takes an array as an input and filters its elements based on a user-defined callback function.
Syntax
The syntax of array_filter()
is as follows:
array_filter(array $array, callable $callback = null, int $flag = 0): array
array
: The input array that needs to be filtered.callback
: An optional parameter that defines the filter function to be applied to each element. If no callback function is provided, all the empty elements in the array will be removed.flag
: An optional parameter that specifies the behavior of the function. By default, it has a value of 0, which means that the callback function will only be applied to the elements that have a truthy value.array
: The output array containing the filtered elements.
Examples
Example 1: Filter Even Numbers
The following example demonstrates how to filter even numbers from an array:
$numbers = array(1, 2, 3, 4, 5, 6); $even_numbers = array_filter($numbers, function($number) { return $number % 2 == 0; }); print_r($even_numbers);
The output of the above code will be:
Array ( [1] => 2 [3] => 4 [5] => 6 )
As you can see, the output array contains only the even numbers 2, 4, and 6.
Example 2: Remove Empty and Null Values
The following example demonstrates how to remove empty and null values from an array:
$fruits = array('apple', 'banana', '', 'orange', null, 'kiwi'); $filtered_fruits = array_filter($fruits); print_r($filtered_fruits);
The output of the above code will be:
Array ( [0] => apple [1] => banana [3] => orange [5] => kiwi )
As you can see, the empty and null values (”, null) have been removed from the array.
Example 3: Filter by Key
You can also use array_filter()
to filter an array by its keys, rather than its values. Here’s an example:
$prices = array( 'apple' => 0.99, 'banana' => 0.25, 'orange' => 0.50, 'kiwi' => 1.50, ); $filtered_prices = array_filter($prices, function($key) { return ($key !== 'banana' && $key !== 'kiwi'); }, ARRAY_FILTER_USE_KEY); print_r($filtered_prices);
This will output:
Array ( [apple] => 0.99 [orange] => 0.5 )
In this example, we’ve filtered out the ‘banana’ and ‘kiwi’ keys from the $prices
array by returning false
from the callback function for those keys.
Example 4: Filter Multidimensional Arrays
The array_filter()
function can also be used to filter multidimensional arrays. Here’s an example:
$products = array( array('name' => 'Apple', 'category' => 'fruit', 'price' => 0.99), array('name' => 'Banana', 'category' => 'fruit', 'price' => 0.25), array('name' => 'Orange', 'category' => 'fruit', 'price' => 0.50), array('name' => 'Milk', 'category' => 'dairy', 'price' => 2.50), array('name' => 'Cheese', 'category' => 'dairy', 'price' => 3.50), array('name' => 'Bread', 'category' => 'grains', 'price' => 1.50), ); $filtered_products = array_filter($products, function($product) { return ($product['category'] === 'fruit' && $product['price'] < 1.00); }); print_r($filtered_products);
This will output:
Array ( [0] => Array ( [name] => Apple [category] => fruit [price] => 0.99 ) [1] => Array ( [name] => Banana [category] => fruit [price] => 0.25 ) [2] => Array ( [name] => Orange [category] => fruit [price] => 0.5 ) )
In this example, we’ve filtered out all products that don’t have a category of ‘fruit’ or a price of less than 1.00.
Conclusion
array_filter()
is a powerful function in PHP that can be used to filter arrays based on a variety of criteria. It can be used to remove empty or null values, filter elements based on a custom callback function, or even filter multidimensional arrays. By using this function, you can significantly reduce the complexity of your code and make it more efficient. I hope this article has given you a good understanding of how array_filter()
works and how it can be used in your PHP projects.
Thanks for reading!