Current Location: Home> Latest Articles> How to implement multilingual default value structure using array_fill_keys?

How to implement multilingual default value structure using array_fill_keys?

M66 2025-06-06

When developing multilingual websites or applications, we often need to set default values ​​for different languages. PHP provides many built-in functions to help developers do this work, and the array_fill_keys function is a very practical tool. This article will introduce how to use the array_fill_keys function to build a default value structure suitable for multilingual support.

What is the array_fill_keys function?

The array_fill_keys function is used to fill an array with the given keys and give the same default values ​​to these keys. Its syntax is as follows:

 array_fill_keys(array $keys, mixed $value): array
  • $keys is an array containing key names.

  • $value is the default value to assign to each key.

For example, the following code generates an array of three keys, each with a value of 'default' :

 $keys = ['en', 'fr', 'de'];
$default_values = array_fill_keys($keys, 'default');
print_r($default_values);

Output:

 Array
(
    [en] => default
    [fr] => default
    [de] => default
)

Default value structure supported by multilingual

In actual development, especially in multilingual support scenarios, we usually need to set different default values ​​for different languages. Using the array_fill_keys function, we can quickly set a unified default value structure for keys in each language. Here is a multilingual support example showing how to use array_fill_keys to build a default translated value structure.

Step 1: Define the language list

Suppose we support three languages: English ( en ), French ( fr ) and German ( de ). We first define an array containing these language keys:

 $languages = ['en', 'fr', 'de'];

Step 2: Build the default value structure

Now we can use the array_fill_keys function to set default values ​​for these languages. Suppose we want the default translation value for all languages ​​to be "Not Available":

 $default_translations = array_fill_keys($languages, 'Not Available');
print_r($default_translations);

Output:

 Array
(
    [en] => Not Available
    [fr] => Not Available
    [de] => Not Available
)

Step 3: Update translation

If we already have some translated content and want to update the translations in certain languages, we just need to directly modify the corresponding value. For example, add specific translations for English ( en ) and French ( fr ):

 $default_translations['en'] = 'Hello';
$default_translations['fr'] = 'Bonjour';
print_r($default_translations);

Output:

 Array
(
    [en] => Hello
    [fr] => Bonjour
    [de] => Not Available
)

Example: Process URL-related content

In some multilingual applications, the URL may also contain language-specific identifiers. For example, we might need to build a URL address for each language that points to the page of the respective language version. Using the array_fill_keys function, we can build a default URL structure for each language.

Suppose we need to set the default URL for all languages ​​to https://www.m66.net/default-page :

 $default_urls = array_fill_keys($languages, 'https://www.m66.net/default-page');
print_r($default_urls);

Output:

 Array
(
    [en] => https://www.m66.net/default-page
    [fr] => https://www.m66.net/default-page
    [de] => https://www.m66.net/default-page
)

Step 4: Update URL

If we want to set different URL addresses for pages in different languages, we can directly update the corresponding value. For example, we set a specific URL for the English version and different URL addresses for the French and German versions:

 $default_urls['en'] = 'https://www.m66.net/en/home';
$default_urls['fr'] = 'https://www.m66.net/fr/accueil';
$default_urls['de'] = 'https://www.m66.net/de/startseite';
print_r($default_urls);

Output:

 Array
(
    [en] => https://www.m66.net/en/home
    [fr] => https://www.m66.net/fr/accueil
    [de] => https://www.m66.net/de/startseite
)

Summarize

Using PHP's array_fill_keys function, we can easily build a default value structure for multilingual applications. This approach not only helps us quickly set default values, but also ensures the simplicity and maintainability of the code. Through this function, we can assign a unified default value to each language, or set different values ​​for different languages ​​according to our needs.

In this article, we show how to set the default translation value and URL address for multilingual applications using array_fill_keys . By using this function properly, we can efficiently manage the content and structure supported by multilingual languages.