
In this tutorial we are going to play around the PHP array filter function called array_filter()
and it is very useful when we want to filter php arrays based on certain condition
PHP array function is a built in function which is use to Filters elements of an array using a callback function.
So basically the array_filter()
function uses the callback function to define the condition based on we want to filter an array.
The callback function is Iterates over each items for the array and if the call back function returns true the current items will be returned to the result of the array filter function.
Table of Contents
Syntax of using array_filter() function:
Here is the syntax of using array function in php, so first parameter is the input array which want to filter and the second parameter is the call function which we used to define condition.
array_filter ( array $input [, callable $callback [, int $flag = 0 ]] ) : array
Last argument is optional parameter which required constants ARRAY_FILTER_USE_KEY
and ARRAY_FILTER_USE_BOTH
3 Best Examples of using Array Filter Function
Let me give you quick and useful example here to work around the array_filter function, so here I am going to create a Post class along with few properties and will create array of Post Objects as showing below:
<?php
class Post
{
public $title;
public $body;
public $author;
public $published;
public function __construct($title, $body, $author, $published)
{
$this->title = $title;
$this->body = $body;
$this->author = $author;
$this->published = $published;
}
}
$posts = [
new Post('Test Post 1', 'Body of post test 1', 'YK', true),
new Post('Test Post 2', 'Body of post test 2', 'YK', false),
new Post('Test Post 3', 'Body of post test 3', 'YK', true),
new Post('Test Post 4', 'Body of post test 4', 'AB', false)
];
If you see we have added four objects of Post
class into the $posts
variables now let’s take quick examples
Filter only Published items from the Array
Here is the example of filtering an array using array_filter
function:
$published = array_filter($posts, function ($post) {
return $post->published;
});
var_dump($published);
If you run the var_dump on this new filter array you should the following result.

Filter only Unpublished items from the Array
Similarly we can use the same function to filter the array but the other direction.
$unpublished = array_filter($posts, function ($post) {
return !$post->published;
});
var_dump($unpublished);

Filter Posts according to the Post Author
This is one more additional example to filter from, here we are filtering based of the author of the post.
$posts = array_filter($posts, function ($post) {
return $post->author == 'AB';
});
var_dump($posts);

Conclusion:
The Most important lesson you should get from this tutorial is to avoid using foreach to filter your array in php
I have given the example using array which is having collection objects but you might have different array
Like array with collection of key value pair, associative or the multidimensional array.
No matter which array you have you can alway use array_filter()
function to filter from depending on you requirements.
Filter Posts according to author?
Yes, but if you set the author to a variable ($author=’AB’) the filter won’t recognise the variable:
$posts = array_filter($posts, function ($post) {
return $post->author == $author’;
});
var_dump($posts);
You should provide the $author variable as showing bellow:
$posts = array_filter($posts, function ($post) use ($author) {
return $post->author == $author’;
});
var_dump($posts);