CKTIS / PHP
array_fill_keys()
Turn the values of a list into keys and initialize every resulting entry with one common value.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
array_fill_keys(array $keys, mixed $value): arrayTurn the values of a list into keys and initialize every resulting entry with one common value. This is convenient for a membership map or an initial status table. Repeated keys collapse to one entry and integer-like keys follow the usual array key conversion rules.
Signature
array_fill_keys(array $keys, mixed $value): array
Example output
{"live":false,"draft":false}
Examples
<?php
$tags = ['live','draft','live'];
echo json_encode(array_fill_keys($tags, false));
Result
{"live":false,"draft":false}