In development, we often need to dynamically build data structures based on the fields passed in by the front end, such as when processing form data, exporting fields, or dynamically configuring the interface to return content. PHP's array_fill_keys function can be used to efficiently initialize an array structure containing specific fields.
This article will use a practical example to explain how to quickly build a reusable data template with array_fill_keys in combination with the field list passed in by the front-end.
array_fill_keys is a function used in PHP to create an array, which accepts two parameters:
array_fill_keys(array $keys, mixed $value): array
$keys : The array to be used as keys.
$value : The default value corresponding to all keys.
The beauty of this function is that it can quickly generate an array with a unified structure without manual looping.
Suppose the front end passes an array of fields, our task is to generate an array template containing the default value (such as null ) based on these fields. This template can be used to fill data, check fields, build response structures, etc.
<?php
// Assume this is the front-end passPOSTList of fields coming
$inputFields = $_POST['fields'] ?? ['id', 'name', 'email'];
// use array_fill_keys Create a default value as null Structure of
$dataTemplate = array_fill_keys($inputFields, null);
// Now you can construct data based on this structure,For example, fill in the user data queried in the database:
$userFromDb = [
'id' => 101,
'name' => 'Alice',
'email' => 'alice@example.com',
'created_at' => '2024-04-01'
];
// Use templates to filter only required fields
$filteredUser = array_intersect_key($userFromDb, $dataTemplate);
// Fill in missing fields(If no fields are transmitted in the database)
$finalUserData = array_merge($dataTemplate, $filteredUser);
print_r($finalUserData);
?>
Array
(
[id] => 101
[name] => Alice
[email] => alice@example.com
)
Sometimes the front end may also send a set of fields to generate link parameters, such as if you want to construct a jump address:
$fields = $_GET['fields'] ?? ['uid', 'token'];
$params = array_fill_keys($fields, '');
// Fill in actual parameters(For example, fromsessionGet it in or in business logic)
$params['uid'] = 888;
$params['token'] = 'abc123';
// structureURL
$queryString = http_build_query($params);
$url = "https://m66.net/api/user_info?" . $queryString;
echo $url;
// Output:https://m66.net/api/user_info?uid=888&token=abc123
Using array_fill_keys allows us to elegantly and efficiently build a unified data structure based on front-end incoming fields. Not only does it simplify the code logic, it also enhances the flexibility and maintainability of the code. Its role is even more indispensable when paired with array functions such as array_merge and array_intersect_key .
Applicable to:
Build a data template
Dynamic field filtering
Parameter initialization
Batch assignment operations, etc.
Make good use of this function to make your PHP code more concise and powerful.