Current Location: Home> Function Categories> array_fill_keys

array_fill_keys

Fill the array with specified keys and values
Name:array_fill_keys
Category:Array
Programming Language:php
One-line Description:Fill the array with the given key value of the specified key name.

array_fill_keys function

Applicable to PHP version: PHP 4 >= 4.0.7, PHP 5, PHP 7, PHP 8

Function description

The array_fill_keys function is used to fill a new array based on the given key array, and the values of all keys are set to the same value.

Function Syntax

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

parameter

  • $keys (array): an array containing all keys to be filled.
  • $value (mixed): The value corresponding to each key can be of any type.

Return value

Returns an array containing all specified keys and values are specified. If the given key array is empty, return an empty array.

Example

$keys = ['a', 'b', 'c'];
$value = 0;
$result = array_fill_keys($keys, $value);
print_r($result);

Description of sample code

In this example, we first define an array $keys containing key names and set the same value $value , i.e. 0 for these keys. Then use the array_fill_keys function to fill the new array. Finally, use print_r to output the result.

Output result

Array
(
    [a] => 0
    [b] => 0
    [c] => 0
)
Similar Functions