The Power of PHP: Understanding the array_fill_keys Function
Learn PHP array_fill_keys(): build an associative array from a key list and a shared fill value, with examples covering duplicates and numeric keys.
array_fill_keys() is a built-in PHP function that builds a new associative array from a list of keys and a single value that every entry shares. It is the fastest way to pre-seed an array when you already know the keys you need but want them all to start from the same default — for example zeroing a set of counters, marking a list of items as false, or creating placeholder slots to fill in later.
This page covers what array_fill_keys() does, its exact behavior with duplicate and numeric keys, how it differs from array_fill() and array_combine(), and when to reach for it.
Syntax
array_fill_keys(array $keys, mixed $value): array| Parameter | Description |
|---|---|
$keys | An array of values to use as the keys of the new array. Each entry is used as a key, not a value. |
$value | The value to assign to every key. Can be any type — a scalar, null, an array, or an object. |
Return value: a new array whose keys come from $keys and whose values are all $value. Available since PHP 5.2.0.
Keys follow PHP's normal array-key rules: only integers and strings are valid. A string that looks like a decimal integer (such as
"5") is cast to the integer5, andtrue/false/nullkeys become1/0/"".
Basic example
Pass the keys as the first argument and the shared fill value as the second:
The output is an associative array using your keys, each mapped to the same value:
Array
(
[a] => value
[b] => value
[c] => value
)This is handy whenever you have a set of keys but the values either don't matter yet or should all start the same — a common case is initializing counters to 0:
<?php
$letters = array("a", "b", "c");
$counts = array_fill_keys($letters, 0);
$counts["a"]++;
$counts["a"]++;
$counts["c"]++;
print_r($counts);
?>Array
(
[a] => 2
[b] => 0
[c] => 1
)Using null, arrays, or objects as the value
The fill value can be any type. Use null to create placeholder slots you'll populate later:
<?php
$keys = array("id", "status");
$filled = array_fill_keys($keys, null);
print_r($filled);
?>Array
(
[id] =>
[status] =>
)When the value is an array or object it is copied by value into each key, so modifying one entry never affects the others:
<?php
$buckets = array_fill_keys(array("a", "b"), array());
$buckets["a"][] = 1;
print_r($buckets);
?>Array
(
[a] => Array
(
[0] => 1
)
[b] => Array
(
)
)Duplicate and numeric keys
Because the result is a real PHP array, the keys obey normal array-key rules. Duplicate keys collapse into a single entry, and decimal-string keys become integers:
<?php
print_r(array_fill_keys(array("5", 5, "name"), null));
?>Array
(
[5] =>
[name] =>
)Here "5" and 5 are both stored as the integer key 5, so only one 5 entry remains. Keep this in mind if your key list might contain duplicates — the array will be shorter than the input.
array_fill_keys() vs. related functions
array_fill()fills a range of sequential integer keys starting at a given offset, rather than from an explicit key list. Use it when you want0, 1, 2, …style indexes.array_combine()pairs a keys array with a values array of equal length, giving each key a different value. Usearray_fill_keys()when every value is the same.array_keys()does the inverse: it extracts the keys out of an existing array.
A common pattern is to seed defaults with array_fill_keys() and then merge real data over the top:
<?php
$defaults = array_fill_keys(array("name", "email", "active"), null);
$input = array("name" => "Ann", "active" => true);
print_r(array_merge($defaults, $input));
?>Array
(
[name] => Ann
[email] =>
[active] => 1
)This guarantees every expected key is present even when the incoming data is partial.
Summary
array_fill_keys() turns a list of keys plus one shared value into a ready-made associative array. Reach for it to initialize counters, flags, or placeholder records in a single line. Remember that the fill value is copied into every key, and that duplicate or decimal-string keys collapse following PHP's standard array-key rules. For different-per-key values use array_combine(); for sequential integer indexes use array_fill(). See the PHP arrays guide for the bigger picture.