When using the GraphQL API to get data, we often deal with nested arrays with complex structures. Since GraphQL does not restrict the case style of field names (such as camelCase, snake_case, etc.), the returned data may have mixed key names in different formats. If you want to unify the case of these key names in PHP, array_change_key_case() is a very practical function.
This article will introduce how to use the array_change_key_case() function in combination with recursive logic when processing data returned by the GraphQL API to achieve the effect of uniformly lowercase or uppercase key names.
array_change_key_case() is a PHP built-in function that converts all key names of an array to lowercase or uppercase.
array_change_key_case(array $array, int $case = CASE_LOWER): array
$case optional parameters:
CASE_LOWER : Default, convert to lowercase
CASE_UPPER : convert to capitalization
Suppose you request the GraphQL interface through the following URL to obtain a list of articles:
$url = 'https://m66.net/graphql';
The JSON data returned by the request is as follows:
{
"data": {
"ArticleList": [
{
"ID": 101,
"Title": "PHP Array Tricks",
"Author": {
"Name": "John Doe",
"Email": "john@example.com"
}
}
]
}
}
You can see that it contains nested arrays of objects, and the key names are inconsistent in the format.
array_change_key_case() itself does not support multi-dimensional arrays, so we need to encapsulate a recursive function to handle nested structures:
function change_keys_case_recursive(array $array, int $case = CASE_LOWER): array {
$result = [];
foreach ($array as $key => $value) {
$newKey = ($case === CASE_UPPER) ? strtoupper($key) : strtolower($key);
if (is_array($value)) {
$result[$newKey] = change_keys_case_recursive($value, $case);
} else {
$result[$newKey] = $value;
}
}
return $result;
}
<?php
// Simulation from GraphQL Interface acquisition JSON data
$json = '{
"data": {
"ArticleList": [
{
"ID": 101,
"Title": "PHP Array Tricks",
"Author": {
"Name": "John Doe",
"Email": "john@example.com"
}
}
]
}
}';
$data = json_decode($json, true);
// Calling a custom function with a unified key name as lowercase
$normalizedData = change_keys_case_recursive($data, CASE_LOWER);
// Output result
print_r($normalizedData);
function change_keys_case_recursive(array $array, int $case = CASE_LOWER): array {
$result = [];
foreach ($array as $key => $value) {
$newKey = ($case === CASE_UPPER) ? strtoupper($key) : strtolower($key);
if (is_array($value)) {
$result[$newKey] = change_keys_case_recursive($value, $case);
} else {
$result[$newKey] = $value;
}
}
return $result;
}
?>
Array
(
[data] => Array
(
[articlelist] => Array
(
[0] => Array
(
[id] => 101
[title] => PHP Array Tricks
[author] => Array
(
[name] => John Doe
[email] => john@example.com
)
)
)
)
)
As you can see, all key names are converted to lowercase, which is more conducive to subsequent data processing and template rendering.
When processing data returned by the GraphQL API, combining array_change_key_case() and recursive logic, it is easy to achieve the uniformity of key names in upper and lower case. Such standardized processing not only improves the readability of the code, but also avoids potential bugs caused by case inconsistencies. You can choose to convert the key name to lowercase or uppercase according to the project needs, and adjust it flexibly.