Current Location: Home> Latest Articles> Is it possible to create nested arrays using the array_fill_keys function? How to do it specifically?

Is it possible to create nested arrays using the array_fill_keys function? How to do it specifically?

M66 2025-05-14

In PHP, array_fill_keys is a very practical function that can quickly fill a set of keys into an array and assign specified values ​​uniformly. Many developers may have this question in their daily use: Can array_fill_keys be used to create nested arrays?

Review of basic usage

Before we dive into nesting arrays, let's take a look at the basic syntax of array_fill_keys :

 array_fill_keys(array $keys, mixed $value): array

Its purpose is to set the same value for each key based on the given array of key names $keys .

Example:

 $keys = ['name', 'email', 'age'];
$result = array_fill_keys($keys, null);
print_r($result);

Output:

 Array
(
    [name] => 
    [email] => 
    [age] => 
)

So, how to create nested arrays?

Although array_fill_keys itself does not automatically generate multi-layer nested structures, you can implement one-layer nesting by passing the value of the array type to it.

Example:

 $keys = ['user1', 'user2', 'user3'];
$nestedStructure = [
    'id' => 0,
    'username' => '',
    'email' => '',
    'profile' => [
        'avatar' => '',
        'bio' => '',
    ]
];

$result = array_fill_keys($keys, $nestedStructure);
print_r($result);

Output:

 Array
(
    [user1] => Array
        (
            [id] => 0
            [username] => 
            [email] => 
            [profile] => Array
                (
                    [avatar] => 
                    [bio] => 
                )

        )

    [user2] => Array
        (
            [id] => 0
            [username] => 
            [email] => 
            [profile] => Array
                (
                    [avatar] => 
                    [bio] => 
                )

        )

    [user3] => Array
        (
            [id] => 0
            [username] => 
            [email] => 
            [profile] => Array
                (
                    [avatar] => 
                    [bio] => 
                )

        )

)

As you can see, we successfully created a preset nested array structure for each user using array_fill_keys .

Notes: Quote issues

One very critical issue to note when using nested arrays: references .

array_fill_keys will copy the array you passed in directly, but if you plan to modify the nested data of one of the users later, be sure to make a deep copy first , otherwise all keys may share the same nested array (especially when you use objects or manually refer to assignments).

Practical application scenarios

You can apply this technique in many practical projects, such as:

  • Initialize the default data structure for multiple users

  • Populate default values ​​for multiple configuration items

  • Build the unified format returned by the interface, for example:

 $apiEndpoints = ['login', 'register', 'logout'];
$responseTemplate = [
    'status' => 'success',
    'data' => [],
    'message' => '',
    'link' => 'https://m66.net/api-docs'
];

$apiResponses = array_fill_keys($apiEndpoints, $responseTemplate);

Summarize

Although array_fill_keys does not directly create "necked arrays", you can completely implement structured initialization by passing in an array containing nested structures as values. This technique is very efficient and concise when building a large amount of data with similar structures.

I hope this article can help you better understand and use the advanced usage of array_fill_keys to improve your PHP encoding efficiency!