Current Location: Home> Latest Articles> Misuse of array_change_key_case() causes loss of key name data

Misuse of array_change_key_case() causes loss of key name data

M66 2025-04-28

In PHP development, array_change_key_case() is a very convenient function that can convert all key names in an array to uppercase or lowercase. But do you know that when this function processes certain arrays, it may cause key name conflicts, thus "silently" losing some data?

Let’s take a specific example to see how this problem occurs.

1. Function introduction

 array_change_key_case(array $array, int $case = CASE_LOWER): array
  • $array : The input array.

  • $case : Optional parameter, default is CASE_LOWER (lower case), or can be set to CASE_UPPER (upper case).

2. Dangerous example: Key name case conflict causes data loss

 $data = [
    'Name' => 'Alice',
    'name' => 'Bob',
    'Age'  => 25,
];

$result = array_change_key_case($data, CASE_LOWER);

print_r($result);

Output result:

 Array
(
    [name] => Bob
    [age] => 25
)

Have you seen it? 'Name' => 'Alice' is gone!
This is because 'Name' and 'name' both become 'name' after being converted to lowercase, causing 'Alice' to be overwritten by 'Bob' !

3. Why is this happening?

array_change_key_case() does not detect whether there is a "case different but actually the same" key name, it simply and roughly replaces the key name with upper and lower case . Once there is a conflict, the behind keys will overwrite the front keys and you won't even receive any warnings or errors.

This is especially dangerous when processing data from multiple sources. For example, when you are doing form merging or API merging, if you do not realize the case conflict between key names, the data will be lost unknowingly.

4. How to avoid this pit?

? Method 1: Manually check conflicts

Before calling array_change_key_case() , you can first detect whether there is a key name case conflict in the original array:

 function hasKeyCaseConflict(array $array): bool {
    $lowerKeys = [];
    foreach ($array as $key => $value) {
        $lower = strtolower($key);
        if (isset($lowerKeys[$lower])) {
            return true;
        }
        $lowerKeys[$lower] = true;
    }
    return false;
}

$data = [
    'Name' => 'Alice',
    'name' => 'Bob',
];

if (hasKeyCaseConflict($data)) {
    echo "There is case conflict between key names,Please process it before converting。";
} else {
    $result = array_change_key_case($data);
    print_r($result);
}

? Method 2: Standardize source data before merging

If you are processing external data, such as content pulled through the API, you can standardize the data before merging, or specify key name rules:

 $apiData = json_decode(file_get_contents('https://m66.net/api/user'), true);

// Assuming the source is reliable,Use it in a unified lower case
$safeData = array_change_key_case($apiData, CASE_LOWER);

5. Summary

Although array_change_key_case() is a simple and practical function, its potential destructiveness cannot be ignored. When there may be duplication of key names in the data you are processing, be sure to think twice before doing it!

?? Tips:

  • The default behavior does not prompt for conflicts!

  • Data may be overwritten "quietly"!

  • The more data integration scenarios, the more careful you need to use them!