PHP Function Reference

PHP array_fill_keys() Function



The PHP array_fill_keys() function fills an array using the values from the keys array as keys and the value equal to value parameter.

Syntax

array_fill_keys(keys, value)

Parameters

keys Required. Specify an array of keys to be used.
value Required. Specify the value to use for filling the array.

Return Value

Returns the filled array.

Exceptions

NA.

Example:

The example below shows the usage of array_fill_keys() function.

<?php
$Arr1 = array("Red", "Green", "Blue",
              "#ff0000", "#33cc33");

//using Arr1 as array of keys and
//'color' as value
$Arr2 = array_fill_keys($Arr1, "color");

//displaying the result
print_r($Arr2);
?>

The output of the above code will be:

Array
(
    [Red] => color
    [Green] => color
    [Blue] => color
    [#ff0000] => color
    [#33cc33] => color
)

❮ PHP Array Reference