The array_fill_keys Function

The array_fill_keys function creates an array and fills it with elements of a specific value so that the entire array has the same elements, but different keys. The keys are taken from the array passed as the first parameter.

Syntax

phparray_fill_keys(array $keys, mixed $value): array

Example

Let's fill an array with elements containing the text 'x'. The keys for the new array will be passed as the first parameter:

php<?php
  $res = array_fill_keys(['a', 'b', 'c'], 'x');
  var_dump($res);
?>

Code execution result:

php['a' => 'x', 'b' => 'x', 'c' => 'x']

See Also

  • the array_fill function,
    which fills an array with values without specified keys