Categories: PHP

Working with PHP Array Filter Function – Best Practices

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.

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:


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;


If you run the var_dump on this new filter array you should the following result.

Use array filter Function to Filter only Published items from the Array

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;

Example of using array filter function in PHP

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';

Filter Posts according to the Post Author


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.

Yogesh Koli

Software engineer & Blogger live in India, has 8+ years of experience working with the Front-end and Back-end Web Application Development.

Recent Posts

Complete guide of using Laravel 6 Eloquent Subquery Enhancements

Learn How to use laravel frameworks new improved feature called Eloquent Subquery and get example…

1 year ago

3 Useful examples of using Array Map function in PHP – Best Practices

Learn how to use php array map function with easy and essential tutorial to modify…

1 year ago

How to add Access Modifiers with Constructor Parameters in TypeScript

Want to know how to refactor your Typescript class, Learn here utilising Typescript of the…

1 year ago

What is Access Modifiers and how to use Access Modifiers in TypeScript ?

What is Access Modifiers in typescript, how to use Access Modifiers, when to use them,…

1 year ago

Top 10 Super Useful Packages to Improve Laravel applications in 2019

This tutorial provide ultimate list of package those are top 10 on packagist and super…

1 year ago

How to secure pdf files In Laravel and allow access to authenticated users

This tutorial provides a perfect solution on how to protect and secure pdf file in…

1 year ago