array_filter() is one of the commonly used functions in PHP, which is used to filter elements in an array. Normally, array_filter() will only return the array value that meets the criteria, and will reset the key name of the array by default, that is, retain the continuous integer key names starting from 0. But sometimes, we want to be able to retain the original key names when filtering the array. This article will explain how to preserve the key names of an array when using array_filter() .
Before discussing how to preserve key names, let's first look at the basic usage of array_filter() . The array_filter() function will iterate over the array and decide whether to retain elements in the array based on the return value of the callback function. If the callback function returns true , the element will be retained, otherwise it will be removed.
<?php
$array = [1, 2, 3, 4, 5];
$result = array_filter($array, function($value) {
return $value % 2 === 0; // Only even numbers are retained
});
print_r($result);
?>
Output result:
Array
(
[1] => 2
[3] => 4
)
As shown above, array_filter() filters the array, but resets the key name. In this example, the output array key name of array_filter() starts at 0 , and we want to preserve the key name of the original array.
PHP provides a common solution, using the third parameter ARRAY_FILTER_USE_KEY of array_filter() . When using this constant, array_filter() executes the callback function based on the key name instead of the value, so that the key name can be preserved when filtering.
<?php
$array = [1 => 10, 2 => 20, 3 => 30, 4 => 40, 5 => 50];
$result = array_filter($array, function($key) {
return $key % 2 === 0; // Keep even keys
}, ARRAY_FILTER_USE_KEY);
print_r($result);
?>
Output result:
Array
(
[2] => 20
[4] => 40
)
In this example, we use ARRAY_FILTER_USE_KEY so that the parameters of the callback function are the keys of the array, not the values. In this way, we can filter the array based on the conditions of the key name while retaining the original key name.
Suppose we have an array containing multiple URL addresses, we want to filter out the addresses containing the m66.net domain name and keep the original key name.
<?php
$urls = [
'a' => 'http://example.com/page1',
'b' => 'https://m66.net/page2',
'c' => 'http://m66.net/page3',
'd' => 'https://example.com/page4',
];
$result = array_filter($urls, function($url) {
return strpos($url, 'm66.net') !== false; // Filter out the inclusion m66.net of URL
});
print_r($result);
?>
Output result:
Array
(
[b] => https://m66.net/page2
[c] => http://m66.net/page3
)
As shown above, array_filter() filters out URL addresses containing m66.net and retains their key names ( b and c ).
The array_filter() function is very powerful and can be used to filter arrays, but by default, the key name of the array is reset. If we want to preserve the key name of the array, we just specify the ARRAY_FILTER_USE_KEY constant when using array_filter() , so that the callback function will accept the key of the array, not just the value.
In this way, you can flexibly filter the array according to the conditions of the key name or value, and retain the original key name to keep the structure of the array intact.