Using PHP's array_filter() Function to Filter Arrays

This article explains how PHP's array_filter() function works, details its parameters, and provides clear code examples for filtering even numbers and removing empty values from associative arrays, helping developers apply flexible array filtering in real-world backend projects.

php Courses
php Courses
php Courses
Using PHP's array_filter() Function to Filter Arrays

In PHP programming, arrays are a fundamental data type, and filtering array elements is a common operation; PHP provides the useful array_filter() function to accomplish this.

The array_filter() function applies a callback to each element of an array, keeping elements that satisfy the condition (callback returns true) and returns a new filtered array.

The function signature is:

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

Parameter description:

$array : the array to be filtered.

$callback : a user‑defined function that receives an element and returns a boolean; true keeps the element, false discards it. If omitted, the function removes elements that are falsey.

$flag : optional, determines how many arguments are passed to the callback. Default 0 passes only the value; a value greater than 0 also passes the key as the second argument.

Below are two examples demonstrating the use of 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);
}

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

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

Running the code outputs:

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

Example 2: Filtering empty values from an associative array

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

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

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

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

The output is:

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

In summary, array_filter() is a versatile PHP function that simplifies array filtering by allowing custom callback logic, making it easy to implement a wide range of filtering scenarios in backend development.

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 Examplearray filteringarray_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.