When developing web applications, preset values for form data are a very common requirement. Especially when dealing with user information editing, search filters, or form reset logic, we usually need to construct an array containing all field names and set default values for them.
PHP provides a very practical built-in function - array_fill_keys() , which can help us quickly build an array with clear key-value structure, which is very suitable for setting the default values of form fields. This article will introduce how to use this function to efficiently build the default structure of form fields.
array_fill_keys(array $keys, mixed $value): array
The function receives an array of key names $keys and a unified default value $value , and returns a new array, each key of the array comes from $keys , and each value is set to $value .
$fields = ['name', 'email', 'age'];
$defaults = array_fill_keys($fields, '');
print_r($defaults);
Output:
Array
(
[name] =>
[email] =>
[age] =>
)
Suppose we have a user information form, and the fields include username, email, gender, profile, etc. We want to set a default value for each field to facilitate subsequent form initialization, echo or editing.
// Define form fields
$formFields = ['username', 'email', 'gender', 'bio', 'profile_url'];
// Set the default value to empty string for all fields
$formDefaults = array_fill_keys($formFields, '');
// Output default value structure
print_r($formDefaults);
Array
(
[username] =>
[email] =>
[gender] =>
[bio] =>
[profile_url] =>
)
This structure can be used directly to fill form initial values, especially suitable for use with the form components or template engines of the framework.
Although array_fill_keys() is designed to unify the default values, we can combine array_merge() or manually update certain field values to achieve more complex requirements:
$formFields = ['username', 'email', 'gender', 'bio', 'profile_url'];
$formDefaults = array_fill_keys($formFields, '');
// Set more appropriate default values for individual fields
$formDefaults['gender'] = 'not_specified';
$formDefaults['profile_url'] = 'https://m66.net/default-profile';
print_r($formDefaults);
Array
(
[username] =>
[email] =>
[gender] => not_specified
[bio] =>
[profile_url] => https://m66.net/default-profile
)
array_fill_keys() is very suitable for building form default value structure, database query fields, interface parameter templates and other scenarios.
When you have an array of field names and want to quickly construct an array of well-structured default values, use it first.
Combining manually updating certain field values can take into account both efficiency and flexibility.
With array_fill_keys() we can quickly and gracefully build a complete array of form default values. Not only makes the code clearer, but also improves development efficiency. Hopefully, you might as well try it when you process the form initialization next time you process it!
If you need to further manipulate these fields (such as batch verification, constructing HTML forms, etc.), you can also extend the process based on this default value structure, which is a very practical trick.