PHP associative arrays use keys to identify values. However, these keys are case-sensitive, meaning "Name" and "name" are treated as different keys.
When working with data from different sources, inconsistent capitalization can make arrays harder to process.
The array_change_key_case() function solves this problem by converting string keys to lowercase or uppercase.
In this guide, you'll learn how the function works, how to handle duplicate keys, and when to use it in practical PHP applications.
What Is array_change_key_case()?
The array_change_key_case() function converts the string keys of an array to lowercase or uppercase.
Its syntax is:
array_change_key_case(
array $array,
int $case = CASE_LOWER
): array
The function accepts two parameters:
$array— The array whose keys you want to convert.$case— Determines whether keys become lowercase or uppercase.
Two constants are available:
| Constant | Behavior |
|---|---|
CASE_LOWER |
Converts string keys to lowercase. This is the default. |
CASE_UPPER |
Converts string keys to uppercase. |
The function returns a new array and does not modify the original array.
Example 1: Converting Keys to Lowercase
By default, array_change_key_case() converts string keys to lowercase.
<?php
$user = [
"NAME" => "Alex",
"EMAIL" => "[email protected]",
"AGE" => 28
];
$result = array_change_key_case($user);
print_r($result);
Output:
Array
(
[name] => Alex
[email] => [email protected]
[age] => 28
)
All string keys are converted to lowercase. The values remain unchanged.
You can also explicitly specify CASE_LOWER:
$result = array_change_key_case(
$user,
CASE_LOWER
);
Both approaches produce the same result.
Example 2: Converting Keys to Uppercase
To convert keys to uppercase, use CASE_UPPER.
<?php
$product = [
"name" => "Keyboard",
"price" => 49.99,
"stock" => 15
];
$result = array_change_key_case(
$product,
CASE_UPPER
);
print_r($result);
Output:
Array
(
[NAME] => Keyboard
[PRICE] => 49.99
[STOCK] => 15
)
This can be useful when preparing data for systems that expect uppercase field names.
What Happens to Numeric Keys?
The function changes only string keys. Numeric keys are left unchanged.
<?php
$data = [
0 => "First",
"NAME" => "Alex",
5 => "Last"
];
print_r(array_change_key_case($data));
Output:
Array
(
[0] => First
[name] => Alex
[5] => Last
)
The integer keys 0 and 5 remain unchanged, while NAME becomes name.
Important: Duplicate Keys Can Overwrite Values
One important behavior of array_change_key_case() is how it handles keys that become identical after conversion.
Consider this example:
<?php
$data = [
"Name" => "Alex",
"NAME" => "Jordan"
];
$result = array_change_key_case(
$data,
CASE_LOWER
);
print_r($result);
Output:
Array
(
[name] => Jordan
)
Both original keys become name. Because PHP arrays cannot contain duplicate keys, the later value replaces the earlier one.
Important: If you need to preserve every value, detect potential key collisions before performing the conversion.
Practical Example: Normalizing Product Data
Imagine your application receives product information from different systems.
One system might use PRODUCT, while another uses Product or product.
Instead of writing separate code for each capitalization, you can normalize the keys before processing the data.
Create a file named normalize-product.php:
<?php
declare(strict_types=1);
// Data received from an external system.
$productData = [
"PRODUCT" => "Wireless Mouse",
"PRICE" => "29.99",
"STOCK" => 12
];
// Normalize all keys to lowercase.
$product = array_change_key_case(
$productData,
CASE_LOWER
);
// Retrieve expected fields.
$name = $product["product"] ?? null;
$price = $product["price"] ?? null;
$stock = $product["stock"] ?? null;
// Validate the data.
if (
!is_string($name) ||
trim($name) === "" ||
!is_string($price) ||
!preg_match('/^/d+/./d{2}$/', $price) ||
!is_int($stock) ||
$stock < 0
) {
exit("Invalid product information.");
}
// Display the product.
echo "<p>Product: " . htmlspecialchars(
$name,
ENT_QUOTES,
"UTF-8"
) . "</p>";
echo "<p>Price: $" . $price . "</p>";
echo "<p>Available: " . $stock . "</p>";
Browser output:
Product: Wireless Mouse
Price: $29.99
Available: 12
After normalization, your application can consistently access product, price, and stock, regardless of their original capitalization.
The example also validates the data before displaying it. Normalizing array keys does not replace normal input validation.
Does array_change_key_case() Work Recursively?
No. The function only changes keys at the top level of the supplied array.
For example:
<?php
$data = [
"USER" => [
"FIRST_NAME" => "Alex"
]
];
$result = array_change_key_case($data);
print_r($result);
Output:
Array
(
[user] => Array
(
[FIRST_NAME] => Alex
)
)
Notice that USER becomes user, but the nested key FIRST_NAME remains unchanged.
If you need to convert keys in nested arrays, you must process those arrays separately or implement a recursive function.
Common Mistakes
| Mistake | Correct Approach |
|---|---|
| Expecting the original array to change | Assign the returned array to a variable. |
| Expecting values to change case | The function changes keys, not values. |
| Ignoring duplicate keys | Check for collisions when preserving all values matters. |
| Expecting nested keys to change | Process nested arrays separately. |
| Expecting full Unicode case conversion | In PHP 8.2+, the built-in conversion only changes ASCII letters. Use an appropriate multibyte-aware approach for Unicode keys. |
Key Takeaways
array_change_key_case()converts string array keys to lowercase or uppercase.CASE_LOWERis the default option.- Use
CASE_UPPERto generate uppercase keys. - Numeric keys and array values remain unchanged.
- The function returns a new array without modifying the original.
- Duplicate keys created by conversion cause earlier values to be overwritten.
- Nested arrays are not processed automatically.
The array_change_key_case() function is a simple way to standardize associative array keys in PHP. It is particularly useful when processing data from external systems that use inconsistent field capitalization.
For additional details, see the official PHP array_change_key_case() documentation.