Understanding array_filter() in PHP

Understanding array_filter() in PHP

Published
Nov 11, 2023
Written by
Benjamin Crozat
0
comments
2 minutes
read

Introduction to array_filter()

array_filter() is a powerful function in PHP. It allows you to filter elements of an array using a callable (a closure for instance). Let me guide you in using this super handy function.

Basic usage of array_filter()

array_filter() works by passing each element of an array through a callback function. If this function returns true, the element is included in the resulting array. This is particularly useful when you need to sift through data and only keep elements that meet certain conditions.

Here’s a simple example:

$array = [1, 2, 3, 4, 5];

$even = array_filter($array, fn ($value) => $value % 2 == 0);

print_r($even);

In this snippet, array_filter() retains only the even numbers from the original array.

Advanced usage of array_filter()

Beyond simple filters, array_filter() can be used in more complex scenarios. For instance, you can filter an array of objects based on the properties of those objects. It’s also possible to use it with associative arrays, filtering by key as well as value.

Common pitfalls when using array_filter()

When using array_filter(), remember that the callback function must return true or false.

$array = [1, 2, 3, 4, 5];

$even = array_filter($array, function ($value) {
    $value % 2 == 0;
});

// []
print_r($even);

If you don’t, the resulting array will be empty.

To finish this up, another common mistake is forgetting that array keys are preserved. This might lead to unexpected gaps in the numeric array indexes:

$array = [1, 2, 3, 4, 5];

$even = array_filter($array, fn ($value) => $value % 2 == 0);

// Array
// (
//     [1] => 2
//     [3] => 4
// )
print_r($even);
About Benjamin Crozat
Benjamin Crozat

Hi! I’m from the South of France and I’ve been a self-taught web developer since 2006. When I started learning PHP and JavaScript, PHP 4 was still widely used, Internet Explorer 6 ruled the world, and we used DHTML to add falling snow on websites.

Being able to educate myself for free on the web changed my life for the better. Giving back to the community was a natural direction in my career and I truly enjoy it.

Therefore, I decided to take action:

  1. I launched this blog in September 2022 with the goal to be in everyone’s Google search. I get more than tens of thousands of monthly clicks from it and even more visits overall (my analytics dashboard is public by the way).
  2. I also started growing my X (formerly Twitter) account at the same time, which has now over 7,000 followers.
  3. All the content I write is free thanks to my sponsors.

I also want to be completely free with my time and make a living with my own products. In April 2024, I launched Nobinge, a tool to summarize and chat with your content, including YouTube videos.

Believe me, I’m just getting started!

0 comments

You need to be signed in to comment this post.
Sign in with GitHub