Current Location: Home> Latest Articles> How to recursively convert keys of multidimensional arrays using array_change_key_case()?

How to recursively convert keys of multidimensional arrays using array_change_key_case()?

M66 2025-04-24

In PHP, array_change_key_case() is a very practical function that converts all key names in an array to lowercase or uppercase. However, this function is only effective for one-dimensional arrays by default. If you have a multidimensional array and want to convert key names at all levels to uppercase or lowercase uniformly, you need to use this function recursively.

This article will describe how to recursively convert all key names of a multidimensional array to lowercase or uppercase using PHP.

Review of basic usage

 $input = ['Name' => 'Alice', 'Age' => 25];
$result = array_change_key_case($input, CASE_LOWER);

// Output:['name' => 'Alice', 'age' => 25]
print_r($result);

The above code can convert all key names of one-dimensional arrays to lowercase. If the array is multidimensional, you cannot use it directly to process the key names of the internal array.

Functions that implement recursive conversion

Below is a custom function that recursively converts array key names of any depth to lowercase or uppercase.

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

    foreach ($array as $key => $value) {
        // Convert the key name of the current layer
        $newKey = ($case === CASE_UPPER) ? strtoupper($key) : strtolower($key);

        // If the value is still an array,Recursive call
        if (is_array($value)) {
            $result[$newKey] = array_change_key_case_recursive($value, $case);
        } else {
            $result[$newKey] = $value;
        }
    }

    return $result;
}

Example: Convert multidimensional array key names to lowercase

 $data = [
    'User' => [
        'Name' => 'Bob',
        'Email' => 'bob@m66.net'
    ],
    'Meta' => [
        'Created' => '2025-04-11',
        'Tags' => ['PHP', 'Array']
    ]
];

$lowerCased = array_change_key_case_recursive($data, CASE_LOWER);

print_r($lowerCased);

Output result:

 Array
(
    [user] => Array
        (
            [name] => Bob
            [email] => bob@vv99.net
        )

    [meta] => Array
        (
            [created] => 2025-04-11
            [tags] => Array
                (
                    [0] => PHP
                    [1] => Array
                )
        )
)

Example: Convert multidimensional array key names to uppercase

 $upperCased = array_change_key_case_recursive($data, CASE_UPPER);

print_r($upperCased);

Output result:

 Array
(
    [USER] => Array
        (
            [NAME] => Bob
            [EMAIL] => bob@vv99.net
        )

    [META] => Array
        (
            [CREATED] => 2025-04-11
            [TAGS] => Array
                (
                    [0] => PHP
                    [1] => Array
                )
        )
)

Summarize

Although array_change_key_case() itself only supports one-dimensional arrays, through recursive methods, we can easily extend its capabilities to handle arrays at any nested hierarchy. This technique is very practical when handling API request data, configuring arrays, and other scenarios.

I hope this article can help you use PHP operand arrays more flexibly. If you have more questions about PHP, feel free to communicate!