Friday, October 24, 2025
HomeLanguagesPHP array_fill_keys() Function

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:




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


Output:

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

Program 2:




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


Output:

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

Program 3:




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


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

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS