PHP array_fill_keys() function

PHP array_fill() function
PHP array_filter() function

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)
ParametersDetails
keysAn array having elements used as keys – required
valueSpecify the value of the key to fill in the array – required
array_fill_keys function in PHP

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.

PHP array_fill() function
PHP array_filter() function
en English
X
Scroll to Top