Home >>PHP Array Functions >PHP array_fill_keys() Function
PHP array_fill_keys() Function is used to create a new array filled with the given keys and value provided as an array to the function. It accepts two parameters, keys and their values to be present in the new array. It returns an array consisting of key-value pairs that are provided to it as parameters.
Syntax:
array_fill_keys($keys, $value);
Parameter | Description |
---|---|
keys | This is a required parameter. This parameter defines an array of values that will be used as keys. |
value | This is a required parameter. This parameter defines the value to use for filling the array. |
Here is an example of array_fill_keys() function in PHP:
<html> <body> <pre> <?php $keys=array("a","b","c","d"); $x=array_fill_keys($keys,"Jerry"); print_r($x); ?> </pre> </body> </html>
Array ( [a] => Jerry [b] => Jerry [c] => Jerry [d] => Jerry )
Example 2:
<html> <body> <pre> <?php $keys=array("1","2","3","4","5","6","7","8","9"); $x=array_fill_keys($keys,"Jerry"); print_r($x); ?> </pre> </body> </html>
Array ( [1] => Jerry [2] => Jerry [3] => Jerry [4] => Jerry [5] => Jerry [6] => Jerry [7] => Jerry [8] => Jerry [9] => Jerry )