PHP array_filter Function

Created with Sketch.

PHP array_filter Function

Summary: in this tutorial, you’ll learn how to use the PHP array_filter() function to filter elements of an array using a callback function.

Introduction to PHP array_filter() function

When you want to filter elements of an array, you often iterate over the elements and check whether the result array should include each element.

The following example uses the foreach statement to iterate over the elements of the $numbers array and filter the odd numbers:

<?php

$numbers = [1, 2, 3, 4, 5];
$odd_numbers = [];

foreach ($numbers as $number) {
if ($number % 2 === 1) {
$odd_numbers[] = $number;
}
}

print_r($odd_numbers);

Code language: HTML, XML (xml)

Output:

Array
(
[0] => 1
[1] => 3
[2] => 5
)

Code language: PHP (php)

The array_filter() function makes the code less verbose and more expressive:

<?php

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

$odd_numbers = array_filter(
$numbers,
function ($number) {
return $number % 2 === 1;
}
);

print_r($odd_numbers);

Code language: HTML, XML (xml)

From PHP 7.4, you can can use the arrow function instead of the anonymous function like this:

<?php

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

$odd_numbers = array_filter(
$numbers,
fn ($number) => $number % 2 === 1
);

print_r($odd_numbers);

Code language: HTML, XML (xml)

The array_filter() function allows you to filter elements of an array using a callback function.

The following ilustrates the syntax of the array_filter() function:

array_filter (
array $array ,
callable|null $callback = null ,
int $mode = 0
) : array

Code language: PHP (php)

The array_filter() function iterates over the elements of the $array and passes each element to the $callback function. If the callback function returns true, the array_filter() function includes the element in the result array.

PHP array_filter() function examples

Let’s take some examples to understand the array_filter() function better.

1) Basic array_filter() function example

To filter out all the odd numbers in the $numbers array, you use the array_filter() function as follows:

<?php

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

$even_numbers = array_filter(
$numbers,
function ($number) {
return $number % 2 === 0;
}
);

print_r($even_numbers);

Code language: HTML, XML (xml)

2) Using callback as a method of a class

Besides a callback, you can pass a method of a class to the array_filter() function like this:

array_filter($items,[$instance,'callback']);

Code language: PHP (php)

For example:

class Odd
{
public function isOdd($num)
{
return $num % 2 === 1;
}
}

Code language: PHP (php)

The Odd class has a method isOdd() that returns true if the argument is an odd number; otherwise, it returns false.

To use the isOdd() method as the callback of the array_filter() function, you use the following form:

<?php

class Odd
{
public function isOdd($num)
{
return $num % 2 === 1;
}
}

$numbers = [1, 2, 3, 4, 5];
$odd_numbers = array_filter(
$numbers,
[new Odd(), 'isOdd']
);

print_r($odd_numbers);

Code language: HTML, XML (xml)

If you have a class that has is a static method, you pass the static method as the callback of the array_filter() function:

array_filter($array, ['Class','callback']);

Code language: PHP (php)

For example:

<?php

class Even
{
public static function isEven($num)
{
return $num % 2 === 0;
}
}

Code language: HTML, XML (xml)

The Even class has a static method isEven() that returns true if the argument is even; otherwise, it returns false.

The following uses the isEven() static method as the callback of the array_fitler() function:

$even_numbers = array_filter($numbers, ['Even','isEven']);

Code language: PHP (php)

From PHP 5.3, if a class implements the __invoke() magic method, you can use it as a callable. For example:

<?php

class Positive
{
public function __invoke($number)
{
return $number > 0;
}
}

Code language: HTML, XML (xml)

In this example, the Positive class has the __invoke() magic method that returns true if the argument is positive; otherwise, it returns false.

You can pass an instance of the Positive class to the array_filter() function for including only positive numbers in the result array.

<?php

class Positive
{
public function __invoke($number)
{
return $number > 0;
}
}

$numbers = [-1, -2, 0, 1, 2, 3];
$positives = array_filter($numbers, new Positive());

print_r($positives);

Code language: HTML, XML (xml)

Output:

Array
(
[3] => 1
[4] => 2
[5] => 3
)

Code language: PHP (php)

Passing elements to the callback function

By default, the array_filter() function passes the value of each array element to the callback function for filtering.

Sometimes, you want to pass the key, not value, to the callback function. In this case, you can pass ARRAY_FILTER_USE_KEY as the third argument of the array_filter() function. For example:

<?php

$inputs = [
'first' => 'John',
'last' => 'Doe',
'password' => 'secret',
'email' => 'john.doe@example.com'
];

$filtered = array_filter(
$inputs,
fn ($key) => $key !== 'password',
ARRAY_FILTER_USE_KEY
);

print_r($filtered);

Code language: HTML, XML (xml)

Output:

Array
(
[first] => John
[last] => Doe
[email] => john.doe@example.com
)

Code language: PHP (php)

In this example, $filtered contains all elements of the $inputs array except for the element whose key is password.

To pass both the key and value of the element to the callback function, you pass the ARRAY_FILTER_USE_BOTH value as the third argument of the array_filter() function. For example:

<?php

$inputs = [
'first' => 'John',
'last' => 'Doe',
'password' => 'secret',
'email' => ''
];

$filtered = array_filter(
$inputs,
fn ($value, $key) => $value !== '' && $key !== 'password',
ARRAY_FILTER_USE_BOTH
);

print_r($filtered);

Code language: HTML, XML (xml)

Output:

Array
(
[first] => John
[last] => Doe
)

Code language: PHP (php)

In this tutorial, you have learned how to use the PHP array_filter() function to filter elements of an array using a callback.

Leave a Reply

Your email address will not be published. Required fields are marked *