In PHP, the array_filter() function is a common array processing tool that is used to filter elements in an array. The array_filter() function accepts a callback function and an array as parameters and returns a new array containing all elements that meet the callback function conditions.
By default, array_filter() retains the key name (subscript) of the filtered array. However, many developers may find that the key names are not retained or some unexpected situations may occur when using them. So, why does this phenomenon occur?
The basic function of array_filter() is to determine which elements are retained based on the return value of the callback function. The element that returns true will be retained, while the element that returns false will be removed. Its syntax is as follows:
array_filter(array $array, callable $callback = null, int $mode = 0): array
$array : The array to be filtered.
$callback : A callback function for filtering. If omitted, all values that are not empty will be retained by default.
$mode : An optional parameter that can control the indexing method of the array, usually ARRAY_FILTER_USE_KEY or ARRAY_FILTER_USE_BOTH .
When we call array_filter() , it retains the original key name of the array element. For example, consider the following example:
$input = [0 => 'apple', 1 => 'orange', 2 => 'banana'];
$result = array_filter($input, function($value) {
return strlen($value) > 5;
});
print_r($result);
The output will be:
Array
(
[1] => orange
[2] => banana
)
As you can see, the original key names (1 and 2) are preserved.
But in some cases, if you find that array_filter() does not retain the key name, it is usually because the ARRAY_FILTER_USE_KEY or ARRAY_FILTER_USE_BOTH mode is used when calling, or the callback function returns a different value.
Let's take a look at this example:
$input = [0 => 'apple', 1 => 'orange', 2 => 'banana'];
$result = array_filter($input, function($key) {
return $key > 0;
}, ARRAY_FILTER_USE_KEY);
print_r($result);
This code will output:
Array
(
[1] => orange
[2] => banana
)
Although we use the ARRAY_FILTER_USE_KEY pattern here, the key names are still preserved because we filter the key values rather than the actual element's values.
array_filter() only filters the values of the array by default, without changing the keys of the array. If you use ARRAY_FILTER_USE_KEY or ARRAY_FILTER_USE_BOTH , the key name may be recalculated or reset. This is due to PHP's internal processing logic, especially when using different filtering modes, the return value may affect the retention of key names.
If you want the filtered array to regenerate consecutive key names, you can use array_values() to rebuild the index:
$input = [0 => 'apple', 1 => 'orange', 2 => 'banana'];
$result = array_filter($input, function($value) {
return strlen($value) > 5;
});
$result = array_values($result);
print_r($result);
Output:
Array
(
[0] => orange
[1] => banana
)
As you can see, after using array_values() , the array keys are reindexed, resulting in successive key names starting from 0.
array_filter() retains key names by default, but key names may occur in specific modes, especially when using ARRAY_FILTER_USE_KEY or ARRAY_FILTER_USE_BOTH . If you need to reset the key name, you can solve it by array_values() .
Hope this article will be helpful for your understanding of array_filter() 's behavior!