Current Location: Home> Latest Articles> Methods to set default values ​​for each key in the array

Methods to set default values ​​for each key in the array

M66 2025-06-06

In PHP, array_fill_keys() is a very useful function that can fill a new array with a given array of key names and set a unified default value for each key. This article will explain how to use this function and give some practical examples.

Introduction to array_fill_keys() function

The array_fill_keys() function accepts two parameters:

  1. keys : an array containing the key names to be filled.

  2. value : The value filled for each key.

The basic usage of a function is to set a unified default value for the specified key and return a new array.

Sample code

Suppose you have an array with multiple key names, and now you want to set a unified default value for those keys.

 <?php
// Define an array containing multiple key names
$keys = ['a', 'b', 'c', 'd'];

// use array_fill_keys Fill in default values ​​for each key
$default_value = 'default_value';
$new_array = array_fill_keys($keys, $default_value);

// Output result
print_r($new_array);
?>

Output:

 Array
(
    [a] => default_value
    [b] => default_value
    [c] => default_value
    [d] => default_value
)

In the above example, array_fill_keys() fills 'default_value' as its default value for each key in the array $keys . The result is a new array returned, each key associated with the default value.

Practical application scenarios using array_fill_keys()

In actual development, there may be scenarios where you need to set the same default value for multiple keys in an array. For example, suppose we have a user form that contains multiple fields (such as username, email, phone, etc.) and want to initialize these fields to an empty string or some default value.

 <?php
// User form field
$form_fields = ['username', 'email', 'phone', 'address'];

// Set the default value to an empty string
$empty_form = array_fill_keys($form_fields, '');

// Output initialized form fields
print_r($empty_form);
?>

Output:

 Array
(
    [username] => 
    [email] => 
    [phone] => 
    [address] => 
)

In this example, we use array_fill_keys() to initialize an array of fields for a user form, each field is filled with an empty string to facilitate subsequent user input.

Use in combination with other functions

The array_fill_keys() function is often used with other PHP array functions. For example, if you need to fill an array and want to perform some filtering, sorting or other processing on the array, you can use the result of array_fill_keys() as input and continue.

Example: Combining array_map() to further process the array value

 <?php
// Set the default value to0
$keys = ['apple', 'banana', 'cherry'];
$default_value = 0;
$new_array = array_fill_keys($keys, $default_value);

// Process the array value,Increase1
$new_array = array_map(function($value) {
    return $value + 1;
}, $new_array);

// Output processed array
print_r($new_array);
?>

Output:

 Array
(
    [apple] => 1
    [banana] => 1
    [cherry] => 1
)

In this example, we first fill the array with array_fill_keys() , and then add 1 to each value in the array through array_map() .

Actual operation of replacing URL domain names

In actual development, if you need to process an array containing URLs, you can use array_fill_keys() and string operation functions to uniformly replace the domain name part in the URL. Here is an example: