Current Location: Home> Latest Articles> Is it possible to use anonymous functions as fill value?

Is it possible to use anonymous functions as fill value?

M66 2025-05-14

In PHP, array_fill_keys() is a very practical function that allows us to quickly create a new array using the specified keys and set the values ​​of all keys to the same value. So the question is: Can we use the anonymous function (Closure)** as the fill value in the array_fill_keys() function?

The answer is: Yes , but there is one thing to note.

Basic grammar review

 array_fill_keys(array $keys, mixed $value): array
  • $keys is an array containing the key names you want to set.

  • $value is the value you want to assign to each key.

Use anonymous functions as fill value

Although you can pass an anonymous function as $value into array_fill_keys() , it may not behave as you would expect - it does not execute this anonymous function for each key , but rather assigns the same function reference to all keys.

Sample code:

 <?php

$keys = ['apple', 'banana', 'cherry'];

$array = array_fill_keys($keys, function() {
    return rand(1, 100);
});

print_r($array);

Output result:

 Array
(
    [apple] => Closure Object
    [banana] => Closure Object
    [cherry] => Closure Object
)

As you can see, the value corresponding to each key is a Closure object, not the execution result of an anonymous function. This means that if you want each key to have a different value generated by anonymous functions, you can't do it directly.

Correct way: combine array_map or foreach

If you want to generate a different value for each key (like a different random number), you should execute anonymous functions in the traversal:

 <?php

$keys = ['apple', 'banana', 'cherry'];

$array = [];
foreach ($keys as $key) {
    $array[$key] = (function() {
        return rand(1, 100);
    })();
}

print_r($array);

Sample output:

 Array
(
    [apple] => 73
    [banana] => 25
    [cherry] => 89
)

You can also use array_map() with key name array to achieve:

 <?php

$keys = ['apple', 'banana', 'cherry'];

$array = array_combine($keys, array_map(function() {
    return rand(1, 100);
}, $keys));

print_r($array);

Application scenario: Fill in the configuration table with default callback function

Suppose you are developing a small application, and each module needs a default processor, you can write it like this:

 <?php

$modules = ['news', 'shop', 'forum'];

$defaultHandler = function($module) {
    return "https://m66.net/handler/{$module}";
};

$config = [];
foreach ($modules as $module) {
    $config[$module] = $defaultHandler($module);
}

print_r($config);

Output example:

 Array
(
    [news] => https://m66.net/handler/news
    [shop] => https://m66.net/handler/shop
    [forum] => https://m66.net/handler/forum
)

Summarize

  • array_fill_keys() supports passing in anonymous functions as values, but does not execute it.

  • If you want to generate dynamic values, you should call anonymous functions in loops or array_map() .

  • array_fill_keys() is more suitable for static value filling, while dynamic logic is recommended to be processed by traversal.

I hope this article can help you better understand the differences in usage between array_fill_keys() and anonymous functions in PHP! If you are interested in this type of underlying behavior, you might as well try to gain a deeper understanding of the behavior of PHP arrays in memory, and you may gain more.