Current Location: Home> Latest Articles> How to make array_change_key_case() affect only part of the keys?

How to make array_change_key_case() affect only part of the keys?

M66 2025-04-24

In PHP, the array_change_key_case() function can easily convert all key names in an array to lowercase or uppercase. But sometimes we don't want to convert all keys of the entire array, but just convert the specified keys in it. At this time, a little trick is needed to achieve this goal.

This article will introduce a method that allows you to "precisely" use the array_change_key_case() function for specified keys in an array.

Question example

Suppose we have the following array:

 $data = [
    'Name' => 'Alice',
    'AGE' => 25,
    'Gender' => 'Female',
    'Country' => 'Canada'
];

We just want to convert the key names of Name and Country to lowercase, and the rest remains the same. The standard array_change_key_case($data, CASE_LOWER) will change all keys, which obviously does not meet our needs.

Solution: Manually operate the specified key

We can iterate over the array, determine whether the current key is in the list we specified, and if so, it will perform case conversion, and then rebuild the array.

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

    foreach ($array as $key => $value) {
        if (in_array($key, $keysToChange)) {
            $newKey = ($case === CASE_UPPER) ? strtoupper($key) : strtolower($key);
        } else {
            $newKey = $key;
        }
        $result[$newKey] = $value;
    }

    return $result;
}

Usage example

 $data = [
    'Name' => 'Alice',
    'AGE' => 25,
    'Gender' => 'Female',
    'Country' => 'Canada'
];

$keysToChange = ['Name', 'Country'];

$modified = change_selected_keys_case($data, $keysToChange, CASE_LOWER);

print_r($modified);

Output result:

 Array
(
    [name] => Alice
    [AGE] => 25
    [Gender] => Female
    [country] => Canada
)

As you can see, only Name and Country are converted to lowercase, and the other keys have not changed.

Additional Instructions

If you need to deal with nested arrays, or if the matching of key names needs to be case-insensitive, you can further optimize the function, such as using strtolower() to compare key names, etc.

You can also transform the function into a more general version, such as supporting callback functions to decide whether to convert a certain key:

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

    foreach ($array as $key => $value) {
        $newKey = $shouldChange($key) ? 
            (($case === CASE_UPPER) ? strtoupper($key) : strtolower($key)) : 
            $key;

        $result[$newKey] = $value;
    }

    return $result;
}

Usage example:

 $modified = change_keys_with_callback($data, function($key) {
    return in_array($key, ['Name', 'Country']);
});

Summarize

Although PHP does not have native support to use array_change_key_case() only for specified keys, we can achieve the same effect through simple traversal and judgment logic. This method is not only flexible, but also allows you to control the conversion logic, which is very suitable for actual development scenarios.