When processing multidimensional arrays in PHP, initializing structures often requires certain skills. array_fill_keys is a very convenient function when initializing a one-dimensional associative array. It allows us to quickly generate an array through an array of key names and a unified initial value. However, when we need to initialize a multidimensional array , the applicability of array_fill_keys becomes worth discussing.
This article will introduce in detail the application methods and applicable scenarios of array_fill_keys in multi-dimensional array initialization, and analyze its limitations.
array array_fill_keys(array $keys, mixed $value)
This function receives two parameters:
$keys : an array containing key names
$value : The value that needs to be assigned to each key
Example:
$keys = ['name', 'age', 'gender'];
$result = array_fill_keys($keys, null);
// Output result
print_r($result);
/*
Array
(
[name] =>
[age] =>
[gender] =>
)
*/
This approach is very suitable for initializing one-dimensional arrays with a unified structure.
Although array_fill_keys itself cannot directly generate nested arrays (that is, it cannot initialize multidimensional structures), we can "disguised" the purpose of initializing multidimensional arrays by combining array_map or loops.
Suppose we need to initialize a unified configuration structure for multiple users:
$user_ids = [101, 102, 103];
$config_keys = ['theme', 'language', 'notifications'];
$default_config = array_fill_keys($config_keys, 'default');
$users = array_fill_keys($user_ids, []);
foreach ($users as $id => &$config) {
$config = $default_config;
}
unset($config);
print_r($users);
/*
Array
(
[101] => Array
(
[theme] => default
[language] => default
[notifications] => default
)
[102] => Array
(
[theme] => default
[language] => default
[notifications] => default
)
[103] => Array
(
[theme] => default
[language] => default
[notifications] => default
)
)
*/
$user_ids = [101, 102, 103];
$config_keys = ['theme', 'language', 'notifications'];
$default_config = array_fill_keys($config_keys, 'default');
$users = array_map(function() use ($default_config) {
return $default_config;
}, array_fill_keys($user_ids, null));
print_r($users);
Initialize configuration data with consistent structure
Quickly create a copy of a 1D array or 2D structure
Form field preset value construction
Nested initialization is not supported : multi-layer nested structures (such as third-level and fourth-level arrays) cannot be generated directly.
Quote Question : If the initialization value is an array or object without a deep copy, multiple keys will point to the same memory area, and modifying one value may affect all other items.
Key values do not support dynamic nesting formats : for dynamic nesting key names, additional logical processing is required.
When array_fill_keys cannot meet the multidimensional array initialization requirements, the following alternatives can be considered:
Building nested arrays using recursive functions
Dynamically generate nested structures with array_reduce
Initialization logical encapsulation using custom classes
Suppose you are building a user permission system, and each user's permission needs to be initialized to an empty collection, you can write it like this: