Current Location: Home> Latest Articles> How to Flexibly Filter Specific Elements in an Array Using PHP's array_filter() Function

How to Flexibly Filter Specific Elements in an Array Using PHP's array_filter() Function

M66 2025-07-29
<?php
// -------------------------------------------------------------
<p>/**</p>
<ul>
<li>
<p>The array_filter() function in PHP is a very powerful built-in function.</p>
</li>
<li>
<p>It allows you to filter the elements of an array using a callback function,</p>
</li>
<li>
<p>returning a subarray that meets the conditions, thus helping developers handle data flexibly.</p>
</li>
<li>
<p>This article will provide a detailed introduction to the usage of array_filter() and some practical filtering techniques.<br>
*/</p>
</li>
</ul>
<p>// 1. Basic usage: Filter out non-empty elements<br>
$input = [0, 1, 2, '', null, false, 'hello', '0'];<br>
$filtered = array_filter($input);<br>
print_r($filtered);<br>
/*<br>
Result:<br>
Array<br>
(<br>
[1] => 1<br>
[2] => 2<br>
[6] => hello<br>
)<br>
*/<br>
// Explanation: When no callback function is provided, array_filter() by default removes all "false-equivalent" values.</p>
<p>// 2. Custom callback function to filter specific conditions<br>
$numbers = [1, 2, 3, 4, 5, 6];<br>
$evenNumbers = array_filter($numbers, function($num) {<br>
return $num % 2 === 0;<br>
});<br>
print_r($evenNumbers);<br>
/*<br>
Result:<br>
Array<br>
(<br>
[1] => 2<br>
[3] => 4<br>
[5] => 6<br>
)<br>
*/<br>
// Explanation: Elements for which the callback function returns true will be retained, and those for which it returns false will be filtered out.</p>
<p>// 3. Filtering by key names<br>
$assoc = [<br>
'apple' => 3,<br>
'banana' => 5,<br>
'cherry' => 1,<br>
'date' => 7,<br>
];<br>
// Retain only elements with keys longer than 5 characters<br>
$filteredByKey = array_filter($assoc, function($value, $key) {<br>
return strlen($key) > 5;<br>
}, ARRAY_FILTER_USE_BOTH);<br>
print_r($filteredByKey);<br>
/*<br>
Result:<br>
Array<br>
(<br>
[banana] => 5<br>
[cherry] => 1<br>
)<br>
*/</p>
<p>// 4. Multi-dimensional array filtering example<br>
$users = [<br>
['name' => 'Alice', 'age' => 23, 'active' => true],<br>
['name' => 'Bob', 'age' => 30, 'active' => false],<br>
['name' => 'Charlie', 'age' => 27, 'active' => true],<br>
];<br>
// Filter users older than 25 and active<br>
$activeAdults = array_filter($users, function($user) {<br>
return $user['age'] > 25 && $user['active'] === true;<br>
});<br>
print_r($activeAdults);<br>
/*<br>
Result:<br>
Array<br>
(<br>
[2] => Array<br>
(<br>
[name] => Charlie<br>
[age] => 27<br>
[active] => 1<br>
)<br>
)<br>
*/</p>
<p>// 5. Considerations and performance tips<br>
// - array_filter() does not reindex the array, preserving the original keys.<br>
// - If sequential indexing is needed, it can be used with array_values.<br>
// - When processing large arrays, try to simplify the callback function logic to avoid performance bottlenecks.</p>
<p data-is-last-node="" data-is-only-node="">// Summary:<br>
// array_filter() is a powerful tool for filtering array elements in PHP. By flexibly defining the callback function,<br>
// it can handle a wide range of filtering requirements, improving code simplicity and readability.<br>
?><br>