array_fill_keys
Fill the array with specified keys and values
Applicable to PHP version: PHP 4 >= 4.0.7, PHP 5, PHP 7, PHP 8
The array_fill_keys function is used to fill a new array based on the given key array, and the values of all keys are set to the same value.
array_fill_keys(array $keys, mixed $value): array
Returns an array containing all specified keys and values are specified. If the given key array is empty, return an empty array.
$keys = ['a', 'b', 'c']; $value = 0; $result = array_fill_keys($keys, $value); print_r($result);
In this example, we first define an array $keys containing key names and set the same value $value , i.e. 0 for these keys. Then use the array_fill_keys function to fill the new array. Finally, use print_r to output the result.
Array ( [a] => 0 [b] => 0 [c] => 0 )