In PHP programming, array operations are one of the most common tasks. Especially when dealing with multi-dimensional arrays or when filtering key-value pairs, PHP provides many functions to simplify this type of operation. Among them, the array_fill_keys and array_intersect_key functions can efficiently help us perform key filtering, especially when specific keys need to be filtered, they can greatly improve the readability and execution efficiency of the code.
array_fill_keys
This function creates a new array whose keys are the values in the specified array, and the values corresponding to all keys will be filled with the same values. This function is very suitable for initializing a new array with a specific key.
array_fill_keys(array $keys, mixed $value): array
$keys : an array of keys, indicating the key you want to use in the new array.
$value : The value corresponding to each key.
This function returns an array containing elements in the original array with the same key as the specified array. It is perfect for extracting the key-value pairs we need from a large array.
array_intersect_key(array $array1, array $array2): array
$array1 : The original array.
$array2 : an array whose keys will be used to match the original array.
Suppose we have an array of multiple user information that contains a large number of keys, and we only want to keep some specific keys. The traditional approach may be to use foreach to traverse arrays and make conditional judgments, while using array_fill_keys and array_intersect_keys can make our code more concise and efficient.
Suppose there is an array containing user information:
$user_info = [
'name' => 'John',
'email' => 'john@example.com',
'age' => 30,
'address' => '123 Main St',
'phone' => '123-456-7890'
];
We only care about the user's name , email and age , and we don't need other keys (such as address and phone ). The following shows how to implement key filtering through array_fill_keys and array_intersect_key .
// Keys to be retained
$keys_to_keep = ['name', 'email', 'age'];
// Create a new array containing the required keys,The value is null
$empty_keys = array_fill_keys($keys_to_keep, null);
// use array_intersect_key Filter out unnecessary keys
$filtered_user_info = array_intersect_key($user_info, $empty_keys);
print_r($filtered_user_info);
Array
(
[name] => John
[email] => john@example.com
[age] => 30
)
Use array_fill_keys($keys_to_keep, null) to create a new array $empty_keys , whose keys are those we need to keep, and the value is null . The function of this array is to serve as a template for filtering keys.
Use array_intersect_key($user_info, $empty_keys) to retain the same elements in the $user_info array as the $empty_keys , thereby filtering out unnecessary keys.
The advantages of this method are:
No need to use a foreach loop.
The filtering of keys can be achieved very concisely.
If the array is large, the performance of array_intersect_key will be more efficient than the foreach loop.
In addition to filtering keys from an array, array_intersect_key can also be used to filter common keys from multiple arrays. Suppose we have two arrays, one containing user information and the other containing administrator permission information, we can use array_intersect_key to get the same key in both arrays.
$user_info = [
'name' => 'John',
'email' => 'john@example.com',
'age' => 30,
'address' => '123 Main St',
'phone' => '123-456-7890'
];
$admin_info = [
'name' => 'John',
'email' => 'john@example.com',
'role' => 'admin'
];
// Get the common key of two arrays
$common_keys = array_intersect_key($user_info, $admin_info);
print_r($common_keys);
Array
(
[name] => John
[email] => john@example.com
)
array_intersect_key compares the keys of two arrays and returns elements with the same key.
By combining array_fill_keys and array_intersect_keys , we can use array key filtering very efficiently. This method not only improves the readability of the code, but also provides better performance when processing large amounts of data. Whether it is filtering keys from one array or extracting common keys from multiple arrays, both functions provide us with great convenience.
I hope this article can help you understand how to use these two functions to perform efficient key filtering operations and improve your PHP programming capabilities!