PHP | array_fill_keys() Function

The array_fill_keys() function is a builtin function in PHP and is used to create a new array filled with the given keys and value provided as an array to the function.

Syntax:

array array_fill_keys ( $keys, $value )

Parameters: This function accepts two parameters, keys and their values to be present in the new array. Both of this parameters are described below:



  1. $keys: This parameter is an array consisting of keys that are to be used for creating the new array. If the $keys array contains any illegal value then it is converted into a string and used.
  2. $value: This parameter can be a single value or a list of values. This parameter represents the value of the keys that are to be inserted into the array. If this parameter is an array, then the new array created will be a 2-d array where each element of $keys array will be a key and every key in this new array will have $value array as a value.

Return value: This function returns an array consisting of key-value pairs that are provided to the function as parameters.

Examples:

Input : $keys = array('golden', 25, 560, 'age')
        array_fill_keys($keys, 'majestic')
Output : Array
        (
           [golden] => majestic
           [25] => majestic
           [560] => majestic
           [age] => majestic
        )

Input :$keys = array('tumult', '25', 560, 'cater')
       array_fill_keys($keys, 'limited')
Output : Array
        (
           [tumult] => limited
           [25] => limited
           [560] => limited
           [cater] => limited
        )

In both the example the keys to be used with the new array are provided as an array to the function and the value to be used is provided as the second argument.

Below programs illustrate the array_fill_keys() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
$keys = array('golden', 25, 560, 'age');
  
// Creating new array with specified keys
$a = array_fill_keys($keys, 'majestic');
  
print_r($a);
  
?>

chevron_right


Output:

Array
(
    [golden] => majestic
    [25] => majestic
    [560] => majestic
    [age] => majestic
)

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
$keys = array('tumult', '25', 560, 'cater');
  
// Creating new array
$a = array_fill_keys($keys, 'limited');
  
print_r($a);
  
?>

chevron_right


Output:

Array
(
    [tumult] => limited
    [25] => limited
    [560] => limited
    [cater] => limited
)

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
$keys = array('tumult', '25', 560, 'cater');
$value = array(5,10);
  
// Creating new array
$a = array_fill_keys($keys, $value);
  
print_r($a);
  
?>

chevron_right


Output:

Array
(
    [tumult] => Array
        (
            [0] => 5
            [1] => 10
        )

    [25] => Array
        (
            [0] => 5
            [1] => 10
        )

    [560] => Array
        (
            [0] => 5
            [1] => 10
        )

    [cater] => Array
        (
            [0] => 5
            [1] => 10
        )
)

Reference:
http://php.net/manual/en/function.array-fill-keys.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.