array_fill_keys
Creates an array with given keys and the same value.
Syntax
array_fill_keys(array $keys, mixed $value): arrayParameters
| Parameter | Type | Description |
|---|---|---|
| $keys | array | The list of keys. |
| $value | mixed | The 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
- array_fill — Creates an array of identical values.
- array_combine — Creates an array from two lists: keys and values.
- array_flip — Swaps keys and values.
In the tutorial
- Array functions — Processing arrays in PHP without loops — array_map, array_filter, array_reduce, searching and slicing.