Current Location: Home> Latest Articles> array_change_key_case() and array_walk_recursive() to process multi-dimensional arrays

array_change_key_case() and array_walk_recursive() to process multi-dimensional arrays

M66 2025-04-24

When processing PHP array data, especially data from external interfaces, you often encounter problems such as inconsistent key names . This situation can cause a lot of trouble when merging, searching, or mapping an array. Fortunately, PHP provides several useful functions, such as array_change_key_case() and array_walk_recursive() , which can help us achieve the goal of unified key name conversion.

But the problem is: array_change_key_case() can only process one-dimensional arrays , while array_walk_recursive() can traverse all values ​​of a multidimensional array, but cannot directly modify the key name. So can we combine these two functions to implement a solution that supports the unified case of key names in multidimensional arrays ?

The answer is: Yes! We can use recursive methods to penetrate into each layer of array and use array_change_key_case() for each layer, thereby implementing key name conversion of the entire multidimensional array.

Sample code: Uniform key name is lowercase

 <?php

function change_array_keys_case_recursive(array $arr, int $case = CASE_LOWER): array {
    $result = [];

    foreach ($arr as $key => $value) {
        $newKey = ($case === CASE_LOWER) ? strtolower($key) : strtoupper($key);

        if (is_array($value)) {
            $result[$newKey] = change_array_keys_case_recursive($value, $case);
        } else {
            $result[$newKey] = $value;
        }
    }

    return $result;
}

// Example array(Contains keys that are mixed with upper and lower case)
$data = [
    'UserName' => 'alice',
    'Email' => 'alice@m66.net',
    'Details' => [
        'Age' => 28,
        'Country' => 'Canada',
        'Preferences' => [
            'Theme' => 'Dark',
            'LANGUAGE' => 'EN'
        ]
    ]
];

// Unify all key names in lowercase
$normalized = change_array_keys_case_recursive($data);

print_r($normalized);
?>

Output result:

 Array
(
    [username] => alice
    [email] => alice@m66.net
    [details] => Array
        (
            [age] => 28
            [country] => Canada
            [preferences] => Array
                (
                    [theme] => Dark
                    [language] => EN
                )
        )
)

Analysis of ideas

  • array_change_key_case() is a PHP-owned tool for processing key name case conversion, but only supports one-dimensional arrays;

  • array_walk_recursive() is suitable for modifying values ​​in an array (but not keys), so it is not ideal for us here;

  • Custom recursive functions are the current optimal solution, which can implement the processing of each layer of array;

  • Strong scalability : Just modify the incoming $case parameter to support CASE_UPPER conversion to uppercase.

Conclusion

Although the functions provided by PHP natively have their own limitations, by reasonably combining and customizing functions , we can build a more powerful and adaptable solution to complex scenarios. For scenarios where external input or cleaning data are required to uniformly process, this method is not only practical, but also has good readability and maintainability.

If you are dealing with complex interface data or frequently encountering inconsistent key names during development, you might as well try this method for unified conversion.