array_fill_keys()
array_fill_keys() — Fill an array with values, specifying keys.
Built-in function of PHP 8.5. The manual lists it as available in PHP 5 >= 5.2.0, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
Fills an array with the value of the value parameter, using the values of the keys array as keys.
Signature
array_fill_keys(array $keys, mixed $value): array
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $keys | array | Array of values that will be used as keys. Illegal values for key will be converted to string. |
| $value | mixed | Value to use for filling |
Return value
Returns the filled array
Example
A runnable snippet:
$keys = array('foo', 5, 10, 'bar'); $a = array_fill_keys($keys, 'banana'); print_r($a);
Output:
Array ( [foo] => banana [5] => banana [10] => banana [bar] => banana )
See also
All PHP 8.5 built-in functions
Reference card built from the PHP manual page array_fill_keys() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.