Current Location: Home> Latest Articles> How to standardize RESTful API data in combination with array_change_key_case()?

How to standardize RESTful API data in combination with array_change_key_case()?

M66 2025-04-24

In development, the data format returned by the RESTful API is usually in JSON or array form. However, the field names returned by the API may be inconsistent with case. At this time, we can use the array_change_key_case() function in PHP to unify the case of the field. This is useful for normalizing data and ensuring consistency, especially when dealing with return values ​​from different APIs.

array_change_key_case() is a very simple function that converts all keys (keys) in an array to lowercase or uppercase. The syntax of this function is as follows:

 array array_change_key_case ( array $array, int $case = CASE_LOWER )
  • $array : The input array.

  • $case : Specifies the case of the conversion, which can be CASE_UPPER (upper case) or CASE_LOWER (lower case). The default value is CASE_LOWER , which means that all keys are converted to lowercase.

Sample code

Suppose the data we get from a RESTful API is as follows (in actual application, you may get the data through file_get_contents() or curl ):

 $data = [
    'Name' => 'John Doe',
    'Email' => 'john.doe@example.com',
    'PhoneNumber' => '123-456-7890',
];

We want to unify all field names in lowercase or uppercase, which can be done using array_change_key_case() . Here is an example of an implementation:

 <?php
// Assume weAPIData obtained
$data = [
    'Name' => 'John Doe',
    'Email' => 'john.doe@example.com',
    'PhoneNumber' => '123-456-7890',
];

// Convert key names of arrays to lowercase
$dataLower = array_change_key_case($data, CASE_LOWER);
print_r($dataLower);

// Convert the key name of the array to uppercase
$dataUpper = array_change_key_case($data, CASE_UPPER);
print_r($dataUpper);
?>

Output:

 // Lowercase key names
Array
(
    [name] => John Doe
    [email] => john.doe@example.com
    [phonenumber] => 123-456-7890
)

// Caps key name
Array
(
    [NAME] => John Doe
    [EMAIL] => john.doe@example.com
    [PHONENUMBER] => 123-456-7890
)

As shown above, array_change_key_case() can easily convert all key names to lowercase or uppercase. This way, you can make sure that the data field names returned by your API meet your specifications, avoiding potential problems caused by case inconsistencies.

Practical application scenarios

Suppose you are developing an application that needs to process data returned from multiple different RESTful APIs, and the returned field names may be inconsistent. You can use array_change_key_case() to standardize the field names to ensure that subsequent code is not affected by these details. for example:

 $responseFromApi = file_get_contents('https://m66.net/api/data');
$data = json_decode($responseFromApi, true);

// Convert all field names to lowercase uniformly
$standardizedData = array_change_key_case($data, CASE_LOWER);

// Continue to follow-up processing

In this way, no matter whether the field name returned by the API is Name , NAME , name , etc., you can handle it uniformly to avoid errors caused by case inconsistency.

Things to note

  1. array_change_key_case() will only modify the key name of the array, but not the value of the array.

  2. If your data is a nested multi-dimensional array, you need to recursively process each layer of array to ensure that all key names are converted to consistent case.

  3. array_change_key_case() is case sensitive for case conversion in strings, so you need to make sure that the parameters you pass in are correct.