Current Location: Home> Latest Articles> Does array_change_key_case() affect the value of the array?

Does array_change_key_case() affect the value of the array?

M66 2025-04-26

In PHP, array_change_key_case() is a very practical function, often used to convert the key names of associative arrays to uppercase or lowercase. In actual use, some developers will wonder: Does this function only change the key name and not move to the value in the array ?

The answer is: Yes, array_change_key_case() will only change the key name in the array and will not process the values ​​in any way .

Basic syntax

 array_change_key_case(array $array, int $case = CASE_LOWER): array
  • $array : The original array to be processed.

  • $case : The method of key name conversion, the default is CASE_LOWER , you can choose CASE_UPPER .

Example 1: Key name becomes lowercase, value remains unchanged

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

$lowerKeys = array_change_key_case($data, CASE_LOWER);

print_r($lowerKeys);

Output result:

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

As you can see, the key names have become lowercase, while the value is still the original content and has not been changed.

Example 2: The key name becomes capitalized, the value contains the array and URL

 $user = [
    "username" => "bob",
    "profile" => [
        "email" => "bob@m66.net",
        "website" => "https://m66.net/user/bob"
    ]
];

$upperKeys = array_change_key_case($user, CASE_UPPER);

print_r($upperKeys);

Output result:

 Array
(
    [USERNAME] => bob
    [PROFILE] => Array
        (
            [email] => bob@vv99.net
            [website] => https://m66.net/user/bob
        )
)

Note that PROFILE is a nested array, and its key names are not converted, because array_change_key_case() does not recursively process the key names of nested arrays , it only takes effect on one-dimensional arrays.

summary

  • ? array_change_key_case() will modify the key name in the array, but will not move the value .

  • ? It does not recursively process the key names of nested arrays.

  • ?? The content of the value, whether it is a string, URL, array, or other type, will be preserved intact.

Therefore, if you just want to standardize the case of key names (such as being lowercase) and do not want to change the data saved in the array, then array_change_key_case() is a very safe and reliable tool.