Using PHP array_filter to Filter Arrays with Practical Examples

This article explains how to use PHP's built-in array_filter function to filter array elements based on custom conditions, detailing its syntax, parameters, and providing three practical code examples that demonstrate filtering odd numbers, strings longer than five characters, and using class method callbacks.

php Courses
php Courses
php Courses
Using PHP array_filter to Filter Arrays with Practical Examples

In PHP development, filtering arrays is a common task, and the built‑in array_filter function provides a flexible way to return a new array containing only elements that satisfy a given condition.

The basic syntax of array_filter is:

array array_filter ( array $array [, callable $callback [, int $flag = 0 ]] )

Parameters: $array: the array to be filtered. $callback (optional): a function that returns true for elements to keep. $flag (optional): controls how the callback receives arguments.

Below are three concrete examples demonstrating different uses of array_filter.

Example 1: Filter odd numbers from an array

$numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

$filteredNumbers = array_filter($numbers, function($value) {
    return $value % 2 == 1;
});

print_r($filteredNumbers);

Output:

Array
(
    [0] => 1
    [2] => 3
    [4] => 5
    [6] => 7
    [8] => 9
)

In this example, an anonymous function checks whether each element is odd.

Example 2: Filter strings longer than five characters

$names = ['John', 'Peter', 'Alice', 'David', 'Sarah'];

$filteredNames = array_filter($names, function($value) {
    return strlen($value) > 5;
});

print_r($filteredNames);

Output:

Array
(
    [2] => Alice
    [3] => David
)

Here the callback uses strlen to keep only strings whose length exceeds five.

Example 3: Use a class method as the callback

class Filter {
    public function isPositive($value) {
        return $value > 0;
    }
}

$numbers = [-1, 2, -3, 4, -5];
$filter = new Filter();

$filteredNumbers = array_filter($numbers, [$filter, 'isPositive']);

print_r($filteredNumbers);

Output:

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

This example defines a Filter class with an isPositive method and passes the method as a callback to array_filter.

In summary, array_filter is a powerful PHP function that can handle simple to complex filtering scenarios using anonymous functions or class methods as callbacks.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPCode Examplesfilteringarray_filter
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.