Current Location: Home> Latest Articles> Why can't array_change_key_case() handle nested arrays?

Why can't array_change_key_case() handle nested arrays?

M66 2025-04-25

In PHP, array_change_key_case() is a practical function that converts all key names in an array to lowercase or uppercase. But many people encounter a problem when using this function for the first time: it cannot handle key names in nested arrays . why is that? How does it work? This article will analyze it for you one by one through examples.

1. Basic usage of this function

Let’s first look at the basic usage of array_change_key_case() :

 $data = [
    "Name" => "Alice",
    "Age" => 25,
    "Email" => "alice@m66.net"
];

$result = array_change_key_case($data, CASE_LOWER);

print_r($result);

Output:

 Array
(
    [name] => Alice
    [age] => 25
    [email] => alice@vv99.net
)

As you can see, all key names have become lowercase. It all looks perfect.

2. When the array has a nested structure...

Let's add a nested array to see the effect:

 $data = [
    "Name" => "Bob",
    "Info" => [
        "Email" => "bob@m66.net",
        "City" => "New York"
    ]
];

$result = array_change_key_case($data, CASE_LOWER);

print_r($result);

Output result:

 Array
(
    [name] => Bob
    [info] => Array
        (
            [Email] => bob@vv99.net
            [City] => New York
        )
)

Have you noticed it? The key name of the Info subarray has not changed! This is because:

array_change_key_case() only takes effect on the first layer key names of the array and does not recursively process nested arrays.

3. How the function works

From the official documentation and source code level, we can know that the essence of array_change_key_case() is to traverse the array and directly manipulate each top-level key name . If a value is an array type, it does not go inside to further recursively modify the key name.

in other words:

 foreach ($array as $key => $value) {
    $newKey = strtolower($key); // or strtoupper($key)
    $newArray[$newKey] = $value;
}

It's that simple and crude! There is no recursion, no in-depth structure, it is very efficient, but it is also very "limited".

4. What should I do if I want to support nesting?

If you want to convert the key names of nested arrays together, you need to write the recursive function yourself. For example:

 function change_keys_case_recursive(array $array, int $case = CASE_LOWER): array {
    $newArray = [];
    foreach ($array as $key => $value) {
        $newKey = ($case === CASE_UPPER) ? strtoupper($key) : strtolower($key);
        $newArray[$newKey] = is_array($value) ? change_keys_case_recursive($value, $case) : $value;
    }
    return $newArray;
}

$data = [
    "Name" => "Charlie",
    "Info" => [
        "Email" => "charlie@m66.net",
        "City" => "London"
    ]
];

$result = change_keys_case_recursive($data, CASE_LOWER);

print_r($result);

Output:

 Array
(
    [name] => Charlie
    [info] => Array
        (
            [email] => charlie@vv99.net
            [city] => London
        )
)

Everything seems much more reasonable now!

5. Summary

  • array_change_key_case() is non-recursive and only acts on the keys on the first layer of the array.

  • If you need to convert keynames to multidimensional arrays, you need to use a custom recursive function.

  • This is also a common problem with many built-in functions in PHP: they are simple and easy to use, but they have limited support for complex structures.

I hope this article can help you understand this problem thoroughly and be more handy the next time you write code to process arrays!