In PHP, you often encounter the need for key case conversion when processing arrays, especially when dealing with associative array data obtained from external sources (such as forms, API returns, etc.). array_change_key_case() is a function used to unify the case of array key names. However, a often overlooked but quite critical issue is:
Does array_change_key_case() change the case of "numeric keys" in the array?
According to the official PHP documentation , the role of array_change_key_case() is:
Returns an array whose keys are converted to uppercase or lowercase.
But this only applies to string type keys, and for numeric keys, the function does not make any modifications.
Here is an example to demonstrate its actual behavior:
<?php
$data = [
"Name" => "Alice",
"EMAIL" => "alice@example.com",
100 => "serial number",
"AGE" => 30
];
$result = array_change_key_case($data, CASE_LOWER);
print_r($result);
?>
Output result:
Array
(
[name] => Alice
[email] => alice@example.com
[100] => serial number
[age] => 30
)
You can see:
All string keys (such as "Name" , "EMAIL" , "AGE" ) are successfully converted to lowercase;
The numeric key 100 remains unchanged, neither becoming a string nor changing case.
This is because numbers are case-free in PHP. The keys of an array in PHP can be strings or integers (integrals). And case conversion is essentially only valid for strings. Therefore, array_change_key_case() will only process elements whose keys are strings.
If you return a set of array data through API requests and want to process key names uniformly, you can use this function. But during the process, you don't need to worry about the keys of the numeric index being incorrectly converted to strings or changing. For example:
<?php
$json = '{
"UserID": 101,
"Username": "bob",
"Scores": {
"0": 90,
"1": 85,
"2": 88
}
}';
$data = json_decode($json, true);
$data = array_change_key_case($data, CASE_LOWER);
// Output JSON Structure to front end
echo json_encode($data);
?>
The front-end can still access data using .scores[0] , without being affected.
If you need to further process nested structures in the array (such as recursively converting key names at all levels to lowercase), you can encapsulate a recursive function, but you still don't need to worry about the number keys:
function array_change_key_case_recursive($arr, $case = CASE_LOWER) {
return array_map(function ($item) use ($case) {
if (is_array($item)) {
return array_change_key_case_recursive($item, $case);
}
return $item;
}, array_change_key_case($arr, $case));
}
? array_change_key_case() is only valid for string keys ;
? It will not modify the numeric keys in the array;
?? The number keys are strictly integers in PHP and there is no upper and lower case theory;
?? If you need to recursively process nested arrays, you need to manually encapsulate the recursive function to process string keys.
In this way, you can use array_change_key_case() with peace of mind during development without worrying about the number keys being processed incorrectly!