Current Location: Home> Latest Articles> array_fill_keys with array_unique to implement key defill

array_fill_keys with array_unique to implement key defill

M66 2025-05-17

How to implement the deduplication and fill function of PHP array keys by combining array_fill_keys and array_unique?

In PHP programming, array operations are very common tasks. In some cases, we need to deduplicate and fill the array. Fortunately, PHP provides two very useful functions: array_fill_keys and array_unique . Using these two functions together, the deduplication and fill functions of array keys can be easily implemented.

This article will explain in detail how to use these two functions to implement this function.

What are array_fill_keys and array_unique ?

  1. array_fill_keys
    The array_fill_keys function is used to fill the keys of an array and assign the same value to each key. Its syntax is as follows:

     array_fill_keys(array $keys, mixed $value): array
    
    • $keys : an array containing the keys to be filled.

    • $value : The value corresponding to each key.

  2. array_unique
    The array_unique function is used to remove duplicate values ​​from an array. Its syntax is as follows:

     array_unique(array $array, int $sort_flags = SORT_STRING): array
    
    • $array : The array to be processed.

    • $sort_flags : Used to specify the type of sort (such as string sorting, numeric sorting, etc.).

Deduplication and fill array keys through these two functions

Suppose we have an array of multiple duplicate keys and we want to deduplicate those keys and fill each key with a specific value.

Sample Scenario:

We have an array of multiple URLs with duplicate URL keys. We want to make sure that there is only one key per URL by deduplication, and then fill each URL key with a value (for example, fill a string "access").

Code example:

 <?php
// Example URL Array
$urls = [
    'https://example1.com' => 'information1',
    'https://example2.com' => 'information2',
    'https://example1.com' => 'information3',
    'https://example3.com' => 'information4',
];

// Replace the domain name as m66.net
$urls = array_map(function($key) {
    return preg_replace('/https?:\/\/[^\/]+/', 'https://m66.net', $key);
}, array_keys($urls));

// Go to the heavy URL key
$uniqueUrls = array_unique($urls);

// use array_fill_keys For each unique URL key填充默认值
$finalArray = array_fill_keys($uniqueUrls, 'access');

// Output result
print_r($finalArray);
?>

Code parsing

  1. Replace URL domain name <br> We first use array_map and preg_replace to replace the URL domain name in the array with m66.net . preg_replace is used to match the domain name part in the URL and replace it.

  2. Deduplication URL key <br> Then, use the array_unique function to de-repeat these URL keys, making sure each URL only appears once in the result.

  3. Fill in default values <br> Finally, use the array_fill_keys function to fill the deduplicated URL key with a uniform default value (for example, "Access").

Output result

After the above processing, the output will be an array unique to each key and all values ​​are "accessed":

 Array
(
    [https://m66.net] => access
    [https://m66.net] => access
    [https://m66.net] => access
)

Through the above method, we can implement deduplication of PHP array keys and fill in specific values.

Summarize

By combining array_fill_keys and array_unique , we can easily implement deduplication and fill functions of PHP array keys. When processing arrays, using these built-in functions can effectively improve the simplicity of the code and execution efficiency. Hopefully the examples and explanations in this article can help you better understand the usage of these two functions.