array_change_key_case()
The PHP Array Change Key Case function is a powerful tool for transforming the keys of an array from one case to another. Whether you need to change the keys of
PHP array_change_key_case() Function
array_change_key_case() returns a copy of an array with all of its string keys converted to either lowercase or uppercase. It is most useful for normalizing keys that come from an unpredictable source — HTTP headers, CSV column titles, database rows, or user input — so the rest of your code can look them up by a single, known casing.
This page covers the syntax, parameters, return value, the gotchas around numeric keys and key collisions, and a few practical recipes.
Syntax
array_change_key_case(array $array, int $case = CASE_LOWER): array| Parameter | Description |
|---|---|
$array | The input array. It is not modified — a new array is returned. |
$case | One of the constants CASE_LOWER (the default) or CASE_UPPER. |
The function returns a new array with the keys re-cased; the original array is left untouched. Only string keys are affected — integer keys are returned unchanged (see Numeric keys are ignored below).
Basic example: uppercasing keys
Output:
Array
(
[FIRST_NAME] => John
[LAST_NAME] => Doe
)The keys are now uppercase while the values (John, Doe) are untouched.
Default behavior: lowercasing
When you omit the $case argument it defaults to CASE_LOWER, so keys become lowercase. This is the most common use — normalizing mixed-case input before you read it:
<?php
$headers = array("Content-Type" => "text/html", "X-Powered-By" => "PHP");
// No second argument → CASE_LOWER
$normalized = array_change_key_case($headers);
echo $normalized["content-type"]; // text/htmlOutput:
text/htmlNow you can always read $normalized["content-type"] regardless of how the header was originally cased.
Numeric keys are ignored
array_change_key_case() only touches string keys. Integer keys pass through unchanged, so an indexed array comes back exactly as it went in:
<?php
$mixed = array("Name" => "Ada", 0 => "zero", "Age" => 36);
print_r(array_change_key_case($mixed, CASE_UPPER));Output:
Array
(
[NAME] => Ada
[0] => zero
[AGE] => 36
)Watch out for key collisions
Because PHP array keys must be unique, re-casing can make two distinct keys collide. When that happens, the later value wins and the earlier entry is silently dropped:
<?php
$array = array("Name" => "Ada", "name" => "Grace");
print_r(array_change_key_case($array, CASE_LOWER));Output:
Array
(
[name] => Grace
)Both "Name" and "name" become "name", so only the last assignment (Grace) survives. If preserving every entry matters, check for case-insensitive duplicates before calling this function.
Only the top level is changed
This function is not recursive — nested array keys are left as-is. To change keys deeper in the structure you must walk the array yourself:
<?php
function changeKeyCaseRecursive(array $array, int $case = CASE_LOWER): array
{
$result = array_change_key_case($array, $case);
foreach ($result as $key => $value) {
if (is_array($value)) {
$result[$key] = changeKeyCaseRecursive($value, $case);
}
}
return $result;
}
$data = array("User" => array("First" => "Ada"));
print_r(changeKeyCaseRecursive($data, CASE_UPPER));Output:
Array
(
[USER] => Array
(
[FIRST] => Ada
)
)Common use cases
- Normalize HTTP headers or query parameters so lookups don't depend on the sender's casing.
- Clean up CSV or spreadsheet column headers before mapping rows to your model.
- Make config or database keys consistent across code that was written by different people.
Related functions
strtolower()andstrtoupper()— change the case of a string value (this function uses them on keys).array_keys()andarray_values()— extract an array's keys or values.array_flip()— swap keys and values.array_map()— transform array values with a callback.- See PHP Arrays for a broader overview.