PHP array_fill_keys() Function | PHP Array Functions

The array_fill_keys() function is used to create an array using specified keys and a single value. This is particularly useful for initializing arrays with default values.

Key Features of array_fill_keys()

Syntax of array_fill_keys()

Syntax


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

Example Using array_fill_keys()

The following example demonstrates how array_fill_keys() is used:

Example


$keys = ["a", "b", "c"];
$value = "PHP";
$array = array_fill_keys($keys, $value);
print_r($array);

Output

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