PHP array_fill_keys() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_fill_keys() creates an array by using the values of a list as keys and giving every key the same value. It is a quick way to build an associative array with default values.

You use array_fill_keys() to set up counters, flags or default settings, for example to give a list of words the starting count 0. Unlike array_combine(), you do not need a second array, because one value is shared by all keys.

Syntax

Call array_fill_keys() with the values you need. Start with this line.

PHP
$counts = array_fill_keys(['php', 'js', 'go'], 0);
CallWhat it does
array_fill_keys(['a', 'b'], 0)Returns ['a' => 0, 'b' => 0].
array_fill_keys(['a', 'b'], [])Returns ['a' => [], 'b' => []], each with its own empty array.
array_fill_keys([], 'x')Returns an empty array.

The values of the first array become the keys. The keys of the first array are ignored. The result keeps the order of the first array.

Library declarations
PHP
array_fill_keys(array $keys, mixed $value): array

Parameters

ParameterDescription
keysRequired. An array whose values will be used as the keys. Values that are not valid keys are converted to strings.
valueRequired. The value given to every key. It can be any type.

Return Value

Return ValueDescription
arrayA new array with each given key set to the value.

PHP Examples

Example 1 — Setting a Default for a List of Keys

Give each key in a list the same starting value.

PHP
<?php
$flags = array_fill_keys(['a', 'b', 'c'], 0);

print_r($flags);

How It Works

  1. The values 'a', 'b' and 'c' of the first array become the keys.
  2. Every key receives the same value, 0.
  3. The order of the keys matches the order of the input list.
  4. print_r() shows the three keys with the value 0.

Example 2 — Duplicate and Numeric Keys

A duplicate key is only created once, and the string '1' becomes the integer key 1.

PHP
<?php
$result = array_fill_keys([1, '1', 'x', 'x'], true);

var_export($result);

How It Works

  1. The integer 1 and the string '1' are the same array key.
  2. The key 'x' appears twice in the input.
  3. Repeated keys overwrite each other, so only one of each is kept.
  4. var_export() shows the result with the integer key 1 and the string key 'x'.

Example 3 — Starting Counters at Zero

Create counters for known names, then update them without checking whether the key exists.

PHP
<?php
$counts = array_fill_keys(['php', 'js', 'go'], 0);

$counts['php']++;
$counts['php']++;
$counts['go']++;

print_r($counts);

How It Works

  1. array_fill_keys() creates three counters that all start at 0.
  2. $counts['php']++ adds 1 to the 'php' counter, and it runs twice.
  3. $counts['go']++ adds 1 to the 'go' counter.
  4. 'js' was never changed, so it still shows 0.

Frequently Asked Questions

It builds an array from a list of keys and one value. Every key in the new array holds that same value.
array_fill() uses consecutive integer keys that begin at a start index. array_fill_keys() uses keys that you provide, so it can create string keys.
array_combine() needs two arrays and gives each key its own value. array_fill_keys() gives all the keys the same value.
Only one entry is created for each distinct key. The result is therefore shorter than the input when the input has repeats.
Yes. For example array_fill_keys(['a', 'b'], []) gives each key its own empty array, which you can then change separately.

Next: array_filter()

array_filter() goes through an array and keeps only the items that pass a test. The test i.

array_filter() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful