Backend Development 5 min read

Using PHP's array_filter() to Filter Arrays

This article explains PHP's array_filter() function, detailing its parameters, usage, and providing two practical examples—filtering even numbers from a numeric array and removing empty values from an associative array—while illustrating the resulting output and highlighting broader applications.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP's array_filter() to Filter Arrays

In PHP programming, arrays are a very common and important data type. One frequently used operation on arrays is filtering elements. PHP provides a very useful function array_filter() to achieve this.

The array_filter() function uses a callback to filter an array, keeping elements that satisfy the condition and returning a new array.

The usage of array_filter() is as follows:

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

Parameter description:

$array : the array to be filtered.

$callback : a callback function that tests each element. It receives the element as a parameter and must return a boolean; true keeps the element, false filters it out. If no callback is provided, elements with false values are removed by default.

$flag : optional, specifies the number of arguments passed to the callback. Default 0 means the callback receives only the element. If greater than 0, the callback receives two arguments: the element and its key.

Below we demonstrate several examples of using array_filter() :

Example 1: Filtering even numbers

<?php
// Original array
$arr = [1, 2, 3, 4, 5, 6];

// Filtering function
function filter_even($value) {
    return ($value % 2 == 0);
}

// Use array_filter()
$new_arr = array_filter($arr, 'filter_even');

// Output result
print_r($new_arr);
?>

Running the above code produces the following output:

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

Example 2: Filtering empty values in an associative array

<?php
// Original array
$arr = ['name' => 'Tom', 'age' => '', 'gender' => 'male', 'email' => ''];

// Filtering function
function filter_empty($value) {
    return ($value !== '');
}

// Use array_filter()
$new_arr = array_filter($arr, 'filter_empty');

// Output result
print_r($new_arr);
?>

Running the above code produces the following output:

Array
(
    [name] => Tom
    [gender] => male
)

The two examples show how array_filter() can be used to filter array elements in different scenarios. In real development, array_filter() has many other applications, and you can flexibly apply it according to your needs.

Summary

The array_filter() function is a very common and practical function in PHP, providing an easy way to filter array elements. By using a callback to determine whether an element meets a condition, various flexible filtering requirements can be achieved. We hope this introduction helps you gain a deeper understanding of array_filter() .

backend developmentPHPData Filteringarray-manipulationarray_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

login 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.