Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

array_fill_keys

Creates an array with given keys and the same value.

Syntax

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

Parameters

ParameterTypeDescription
$keysarrayThe list of keys.
$valuemixedThe value of every element.

Return value

arrayThe filled array.

Examples

Counters starting at zero:

<?php
 
echo json_encode(array_fill_keys(['php', 'js', 'python'], 0));

Output:

{"php":0,"js":0,"python":0}

Notes

Handy for counting when the set of keys is known in advance and must appear in the result even with a zero.

See also

In the tutorial

  • Array functions — Processing arrays in PHP without loops — array_map, array_filter, array_reduce, searching and slicing.

Thanks for reading!

Donate to the site — we work hard for you.