Using array_filter() to Filter Arrays in PHP
This article introduces PHP's array_filter() function, explains its parameters and usage, and demonstrates two practical examples—filtering even numbers from a numeric array and removing empty values from an associative array—showcasing how callbacks enable flexible array filtering in backend development.
In PHP programming, arrays are a common data type, and filtering array elements is a frequent operation. PHP provides the useful array_filter() function for this purpose.
The array_filter() function applies a callback to filter an array, keeping elements that satisfy the condition and returning a new array.
Usage syntax:
array array_filter ( array $array [, callable $callback [, int $flag = 0 ]] )Parameters:
$array: the array to be filtered.
$callback: a callable that receives each element and returns a boolean; true keeps the element, false discards it. If omitted, elements evaluating to false are removed.
$flag (optional): determines how many arguments are passed to the callback. Default 0 passes only the value; a value >0 passes both value and key.
Below are two examples demonstrating the use of array_filter():
Example One: 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 code outputs:
Array
(
[1] => 2
[3] => 4
[5] => 6
)Example Two: 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 !== '');
}
// Use array_filter()
$new_arr = array_filter($arr, 'filter_empty');
// Output result
print_r($new_arr);
?>Running the code outputs:
Array
(
[name] => Tom
[gender] => male
)These examples show two scenarios of using array_filter() to filter array elements. In real development, the function has many other applications, and developers can apply it flexibly according to their needs.
Summary
The array_filter() function is a widely used and practical tool in PHP that provides a convenient way to filter array elements. By using a callback to test each element, developers can implement various flexible filtering requirements, deepening their understanding of array handling in backend development.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
