Current Location: Home> Latest Articles> Detailed explanation of the basic usage method of array_fill_keys

Detailed explanation of the basic usage method of array_fill_keys

M66 2025-06-06

In PHP, array_fill_keys is a very useful array function. It can be used to create a new array where the keys are generated by the given array, and the value of each key is the specified value. In short, array_fill_keys allows you to fill an array of specified keys with the same value.

grammar

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

parameter:

  • $keys : This is an array containing all keys that need to be filled.

  • $value : This is the value used for padding, and all keys will use this value.

Return value:

This function returns a new array, the keys of the array are provided by $keys , and the value corresponding to each key is $value .

Sample code

Here is an example showing how to use the array_fill_keys function:

 <?php
// Define an array of key names
$keys = ['apple', 'banana', 'cherry'];

// Define the value of the fill
$value = 'fruit';

// use array_fill_keys Fill key
$filledArray = array_fill_keys($keys, $value);

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

Output:

 Array
(
    [apple] => fruit
    [banana] => fruit
    [cherry] => fruit
)

In this example, the $keys array contains three elements: apple , banana , and cherry , and $value is set to fruit . The array_fill_keys function will create a new array, the keys of the array are elements in $keys , and the value of each key is 'fruit' .

Practical application scenarios

array_fill_keys is very useful in many cases, especially when you need to initialize an array with the same value. For example, suppose you are working on the user's permission settings, you can use array_fill_keys to initialize all permissions to the default value:

 <?php
// Keys that define user permissions
$permissions = ['read', 'write', 'execute'];

// Default permission value
$defaultPermission = false;

// Initialize permission array
$userPermissions = array_fill_keys($permissions, $defaultPermission);

// Output user permissions
print_r($userPermissions);
?>

Output:

 Array
(
    [read] => 
    [write] => 
    [execute] => 
)

In this example, we initialize the default value false for each permission ( read , write , and execute ), indicating that the user does not have these permissions. After that, you can modify certain permissions as needed.

Used in conjunction with other functions

array_fill_keys can also be used in conjunction with other PHP array functions for more flexibility in handling arrays. For example, you can extract keys from an existing array via array_keys and then use array_fill_keys to create a new array.

 <?php
// Example array
$array = ['a' => 1, 'b' => 2, 'c' => 3];

// Extract key
$keys = array_keys($array);

// use array_fill_keys Fill in a new array
$newArray = array_fill_keys($keys, 0);

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

Output:

 Array
(
    [a] => 0
    [b] => 0
    [c] => 0
)

In this example, firstly, the keys of the original array are extracted through array_keys , and then a new array is created using array_fill_keys , where the values ​​of all keys are filled with 0.