What is array_filter in PHP
Description
PHP array_filter function filters array elements using a callback function.
Function Signature
array_filter(array $array, ?
callable
$callback = null, int $mode = 0): array
Arguments
$array
– The array to filter$callback
– A user-defined function$mode
– A flag that determines the callback function’s parameter
Note
The $mode accepts the following flag values.
ARRAY_FILTER_USE_KEY
– pass key as the only argument to callback instead of the valueARRAY_FILTER_USE_BOTH
– pass both value and key as arguments to callback instead of the value
By default, the function passes only the key values to the callback function.
Return Type
The function returns a filtered array.
Introduction
PHP array filter
is probably one of the most important PHP functions. It is a higher-order function. A higher-order function either accepts a function as an argument or returns one. Similarly, array_filter accepts a callback function argument and filters an array based on that.

PHP array_filter function filters an array by passing an array’s keys, values, or both to the callback function. The callback function has some logic, and it is expected to return either True
or False
. If True, the function retains the value, and it becomes part of the resulting output array.
That’s a brief description of the function. Let’s see it through examples for more clarity.
Use array_filter to filter negative values from an array
Here’s a straightforward example. The array_filter function here filters out negative values from a PHP array.
<?php
$nums = [-10000,-1000,-100,-10,10,100,1000,10000];
function positiveInts($num) {
//Returns true if the $num is positive
return $num > 0;
}
$filtered_arr = array_filter($nums,'positiveInts');
print_r($filtered_arr);
/*
OUTPUT
Array
(
[4] => 10
[5] => 100
[6] => 1000
[7] => 10000
)
*/
?>
Voila! We have passed a callback function by the name positiveInts. This function returns true if the passed integer is positive. What array_filter does is iterates over the $nums array and passes the values one by one to the callback function.
It filters out the array values for which the callback returns false and retains those for which it returns true. Observe that we have passed the callback function’s name in the string. Here’s another way to declare the callback within the array_filter.
<?php
$nums = [-10000,-1000,-100,-10,10,100,1000,10000];
function positiveInts($num) {
}
$filtered_arr = array_filter($nums,function($v) {
return $v > 0;
});
print_r($filtered_arr);
/*
OUTPUT
Array
(
[4] => 10
[5] => 100
[6] => 1000
[7] => 10000
)
*/
?>
See! Also, observe that the resulting PHP array is not reindexed. So, for resetting index you can use the array_values function in PHP.
Filter array with filter_array using keys and values.
The previous example was straightforward. Let’s see a more complex PHP array and filter it out using keys and values. Let’s see the good old developers
array.
$developers = [
"PHP" => [
"Nancy" => ["Experience(years)"=>3,"Skills"=>["Laravel","GIT","HTML","CSS"]],
"Josh" => ["Experience(years)"=>2,"Skills"=>["MVC","GIT","Docker","Bootstrap","CSS"]],
],
"Java"=>[
"Bruce" => ["Experience(years)"=>5,"Skills"=>["Java Spring","Docker","GIT","Flutter"]],
"Karen" => ["Experience(years)"=>2,"Skills"=>["Java Spring","GIT","Android"]],
],
"Javascript"=>[
"Adam" => ["Experience(years)"=>3,"Skills"=>["React","HTML","CSS","React Native"]],
"Micheal" => ["Experience(years)"=>5,"Skills"=>["React","Node","Express","React Native"]],
],
"Python"=>[
"Ajay" => ["Experience(years)"=>4,"Skills"=>["TensorFlow","Pytorch","Pandas"]],
],
"C++"=>[
"Allan" => ["Experience(years)"=>5,"Skills"=>["RasberryPI"]],
]
];
We have a nested structure, and we are going to filter it using the array_filter function. So, we will be searching for a developer with “Node.js” skills.
$filtered_arr = [];
foreach($developers as $lang=>$developers) {
$temp = array_filter($developers,function($developer) {
//Returns a true is skills include Node.
return in_array("Node",$developer["Skills"]);
});
if($temp){
array_push($filtered_arr,$temp);
}
}
print_r($filtered_arr);
/*
OUTPUT
Array
(
[0] => Array
(
[Micheal] => Array
(
[Experience(years)] => 5
[Skills] => Array
(
[0] => React
[1] => Node
[2] => Express
[3] => React Native
)
)
)
)
*/
Here’s the example and the output. It finds the Node developer for us among the huge developers pool. The foreach array iterates over the external arrays.
Use the ARRAY_FILTER_USE_KEY mode in array_filter
We have seen the $mode argument already. It is a flag to indicate the function that it should be passing to the callback function. Essentially the callback gets the value in PHP arrays only. However, we can pass the keys, too, by using the $mode. Here’s how.
<?php
$regions = [
"South Asia"=> "10,000",
"Central Asia" => "20,000",
"South East Asia" => "55,000",
"Australia"=>"60,000",
"Central Europe" => "80,000",
"South America" => "100,000"
];
$filtered_arr = array_filter($regions,function($k) {
//Return Asia region only
return str_contains($k,"Asia");
},ARRAY_FILTER_USE_KEY);
print_r($filtered_arr);
/*
OUTPUT
Array
(
[South Asia] => 10,000
[Central Asia] => 20,000
[South East Asia] => 55,000
)
*/
?>
Because we need the kye value to implement the logic, we had to alter the function behavior to make it pass key instead of PHP array value. So, we have used ARRAY_FILTER_USE_KEY. You can pass key and value by using the ARRAY_FILTER_USE_BOTH flag.
Conclusion
In this article, we’ve seen the array_filter in depth. We have reviewed the function specifications and explored them further through examples. The function gets a callback, which is a function that accepts PHP array values, as the array_filter iterates over the array. This behavior can be altered using different flags. That’s all we have seen, and we hope you have a good idea of the array_filter function now. Stay tuned for more interesting PHP articles.
Want to get better consulting work?
I am a cloud architect consultant and have been freelance developing for close to 20 years. I have learned a lot along the way. I have started a free weekly newsletter that you can join and learn from my practical and real-world experiences. I’m not a millionaire but I am living comfortably and getting good consistent work on my own terms. If you want to get consistently great consulting work then you should sign up.
Sign up for my free weekly newsletter to get better web development work