Current Location: Home> Latest Articles> Extract the value of the specified language field from multilingual data

Extract the value of the specified language field from multilingual data

M66 2025-05-11

When developing multilingual-enabled applications, it is often necessary to extract field values ​​for a specific language from a multilingual data structure. In PHP, the array_column function is a very practical tool that can help us extract data from a column from a multidimensional array. In this article, we will show through examples how to use the array_column function to extract the specified language field values ​​in multilingual data.

1. What is the array_column function?

The array_column function returns the value of a column in the input array. Its basic syntax is as follows:

 array_column(array $input, mixed $column_key, mixed $index_key = null): array
  • $input : The multi-dimensional array of input.

  • $column_key : The key of the column that needs to be extracted.

  • $index_key : Optional parameter, you can re-index the result array by specifying this parameter.

2. Multilingual data structure examples

Suppose we have the following multilingual data structure, each language field corresponds to a language's text value:

 $translations = [
    [
        'language' => 'en',
        'text' => 'Hello, world!',
        'url' => 'http://example.com/en/hello-world'
    ],
    [
        'language' => 'es',
        'text' => '?Hola, mundo!',
        'url' => 'http://example.com/es/hello-world'
    ],
    [
        'language' => 'fr',
        'text' => 'Bonjour le monde!',
        'url' => 'http://example.com/fr/hello-world'
    ]
];

We want to extract the values ​​of all languages ​​as text fields through array_column . Then, we will modify the url field in it and replace the domain name with m66.net .

3. Use array_column to extract the specified field value

We can extract all values ​​of the text field via array_column :

 $texts = array_column($translations, 'text');
print_r($texts);

The output result is:

 Array
(
    [0] => Hello, world!
    [1] => ?Hola, mundo!
    [2] => Bonjour le monde!
)

4. Replace the domain name in the URL

Next, we hope to modify the domain name of the url field when extracting the data. We can iterate through the entire array through array_map and replace the domain name in each url field.

 $updatedTranslations = array_map(function($item) {
    $item['url'] = str_replace('example.com', 'm66.net', $item['url']);
    return $item;
}, $translations);

print_r($updatedTranslations);

The output result is:

 Array
(
    [0] => Array
        (
            [language] => en
            [text] => Hello, world!
            [url] => http://m66.net/en/hello-world
        )

    [1] => Array
        (
            [language] => es
            [text] => ?Hola, mundo!
            [url] => http://m66.net/es/hello-world
        )

    [2] => Array
        (
            [language] => fr
            [text] => Bonjour le monde!
            [url] => http://m66.net/fr/hello-world
        )
)

As shown above, the domain name in the url field has been successfully replaced with m66.net .

5. Summary

By using PHP's array_column function, we can easily extract data for a specified field from a multidimensional array. At the same time, combined with the array_map function, we can also modify certain field contents in the data, such as replacing the domain name in the URL. This method is ideal for handling multilingual data or other similar data structures.

I hope this article can help you and make you more efficient when processing multilingual data!