In PHP, the case of array key names can cause some inconsistency when processing data, especially when processing data from JSON data or APIs. In order to unify the format of key names, we can use the array_change_key_case() function to unify the array key names into lowercase or uppercase.
When we are dealing with nested arrays or objects, relying solely on array_change_key_case() is not enough. At this time, we can use json_encode() and json_decode() to achieve deeper processing. This article will use a simple example to show how to implement unified conversion of array key names.
This function can convert all key names in an array to uppercase or lowercase.
array_change_key_case(array $array, int $case = CASE_LOWER): array
The parameter $case can be set to:
CASE_LOWER (default): Convert to lowercase
CASE_UPPER : Convert to uppercase
These two functions can convert PHP arrays and JSON strings to each other, and are often used for data transmission and processing. Encoding the data into JSON and then decoding it into an array allows for easy in-depth operation, especially recursive processing of key names.
Suppose we get the following data from the interface https://api.m66.net/user/profile :
$data = [
"UserID" => 123,
"UserName" => "Alice",
"ContactInfo" => [
"Email" => "alice@m66.net",
"Phone" => "1234567890"
]
];
We want to convert all key names (including nested) to lowercase uniformly, and the solution is as follows:
function array_change_key_case_recursive(array $arr, int $case = CASE_LOWER): array {
$result = [];
foreach ($arr as $key => $value) {
$key = ($case === CASE_UPPER) ? strtoupper($key) : strtolower($key);
if (is_array($value)) {
$result[$key] = array_change_key_case_recursive($value, $case);
} else {
$result[$key] = $value;
}
}
return $result;
}
// Sample data
$data = [
"UserID" => 123,
"UserName" => "Alice",
"ContactInfo" => [
"Email" => "alice@m66.net",
"Phone" => "1234567890"
]
];
// Convert to lowercase key name
$lowercased = array_change_key_case_recursive($data, CASE_LOWER);
echo json_encode($lowercased, JSON_PRETTY_PRINT);
The output result is as follows:
{
"userid": 123,
"username": "Alice",
"contactinfo": {
"email": "alice@m66.net",
"phone": "1234567890"
}
}
You might ask: Why not use array_change_key_case() directly? The main reason is that it cannot recursively process nested arrays . Through json_encode() and json_decode() , objects can be converted into array formats, and then the key names of each layer can be recursively processed.
For example, what we receive from https://api.m66.net/data/info is an object structure, which can be converted and processed using the following method:
$json = '{
"UserID": 456,
"UserProfile": {
"FullName": "Bob",
"Contact": {
"Email": "bob@m66.net"
}
}
}';
$array = json_decode($json, true); // Convert to an array
$normalized = array_change_key_case_recursive($array, CASE_LOWER);
echo json_encode($normalized, JSON_PRETTY_PRINT);
By using array_change_key_case() with json_encode() and json_decode() , we can easily implement unified case conversion of multi-layer nested array key names. This approach is especially practical when processing API data, configuration items, or large data structures, and can significantly improve the consistency and robustness of data processing.
If you need to deal with key name conversion or unified format, you might as well add this trick to your PHP toolbox!
Related Tags:
json_encode