Using array_filter() to Filter Arrays in PHP
This article explains the PHP array_filter() function, its parameters, and demonstrates how to filter numeric arrays and associative arrays with practical code examples, helping developers efficiently remove unwanted elements based on custom callback logic.
In PHP programming, arrays are a common and important data type, and filtering array elements is a frequently used operation. PHP provides a very useful function array_filter() to achieve this.
The array_filter() function applies a callback to each element of an array, keeping elements that satisfy the condition and returning a new array.
The usage of the function is as follows:
array array_filter ( array $array [, callable $callback [, int $flag = 0 ]] )Parameter description:
$array: the array to be filtered;
$callback: a callable that tests each element. It receives the element as a parameter and must return a boolean; true keeps the element, false removes it. If omitted, the function removes elements that are falsey.
$flag (optional): specifies how many arguments are passed to the callback. The default 0 means the callback receives only the element. A value greater than 0 makes the callback receive the element and its key.
Below are several examples demonstrating the use of array_filter():
Example 1: Filter Even Numbers from an Array
<?php<br/>// Original array<br/>$arr = [1, 2, 3, 4, 5, 6];<br/><br/>// Filter function<br/>function filter_even($value) {<br/> return ($value % 2 == 0);<br/>}<br/><br/>// Use array_filter()<br/>$new_arr = array_filter($arr, 'filter_even');<br/><br/>// Output result<br/>print_r($new_arr);<br/>?>Running the above code outputs:
Array<br/>(<br/> [1] => 2<br/> [3] => 4<br/> [5] => 6<br/>)Example 2: Filter Empty Values from an Associative Array
<?php<br/>// Original array<br/>$arr = ['name' => 'Tom', 'age' => '', 'gender' => 'male', 'email' => ''];<br/><br/>// Filter function<br/>function filter_empty($value) {<br/> return ($value !== '');<br/>}<br/><br/>// Use array_filter()<br/>$new_arr = array_filter($arr, 'filter_empty');<br/><br/>// Output result<br/>print_r($new_arr);<br/>?>Running the above code outputs:
Array<br/>(<br/> [name] => Tom<br/> [gender] => male<br/>)The two examples show how array_filter() can be used to filter even numbers in a numeric array and to remove empty values in an associative array. In real development, array_filter() has many other use cases, and developers can apply it flexibly according to their needs.
Summary
The array_filter() function is a widely used and practical PHP function that provides a convenient way to filter array elements. By using a callback to determine whether each element meets a condition, various flexible filtering requirements can be achieved. We hope this introduction helps you gain a deeper understanding of using array_filter() in PHP.
PHP Practical Development Rapid Entry
Scan the QR code to receive free learning materials
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.
