PHP 8 reference · Array Functions

PHP array_fill_keys() Function

Create an array from a list of keys, giving every key the same value. Useful for defaults and counters.

PHP 8 Since 5.2

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

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

Parameters

ParameterDescription
keys The keys to use.
value The value for every key.

Return value

The new array.

Examples

Start counters at 0Try it yourself
<?php
print_r(array_fill_keys(["tea", "cake", "milk"], 0));

Output

Array
(
    [tea] => 0
    [cake] => 0
    [milk] => 0
)
Default form valuesTry it yourself
<?php
$fields = ["name", "email", "phone"];
$form = array_merge(array_fill_keys($fields, ""), ["name" => "Ada"]);
print_r($form);

Output

Array
(
    [name] => Ada
    [email] => 
    [phone] => 
)

Version history

VersionChange
PHP 5.2Added

In other languages

Was this page helpful?