In the previous tutorial, we learned about the array_fill function that simply fills an array with values. The array_fill_keys function fills an array with values against the specified keys.
What is the syntax of the array_fill_keys function in PHP?
array_fill_keys(keys, value)
Parameters | Details |
---|---|
keys | An array having elements used as keys – required |
value | Specify the value of the key to fill in the array – required |
Example of array_fill_keys function
<?php
$keys=array("a","b","c","d");
$a1=array_fill_keys($keys,"blue");
print_r($a1);
?>
In the above example, we created an array that will serve as the keys for the array_fill_keys function. Then we passed the array to the function and specify the value to be assigned to our keys. The output of the above example will be an array containing our values and the specified string as their values.