Computer >> Computer tutorials >  >> Programming >> PHP

array_fill_keys() function in PHP


The array_fill_keys() function fill an array with values, specifying keys. It returns the filled array.

Syntax

array_fill_keys(keys, values)

Parameters

  • keys − Array of values to be used as keys.

  • values − Values that would be used to fill.

Return

The array_fill_keys() function returns the filled array.

Example

<?php
   $arr = array('p', 'q', 'r');
   $res = array_fill_keys($arr, demo);
   print_r($res)
?>

Output

Array (
   [p] => demo
   [q] => demo
   [r] => demo
)

Let us see another example.

Example

<?php
   $arr = array('a', 'b', 'c', 1, 2, 3, 'steve', 'tom', 4, 5);
   $res = array_fill_keys($arr, demo);
   print_r($res)
?>

Output

Array (
   [a] => demo
   [b] => demo
   [c] => demo
   [1] => demo
   [2] => demo
   [3] => demo
   [steve] => demo
   [tom] => demo
   [4] => demo
   [5] => demo
)