Current Location: Home> Latest Articles> How to use array_change_key_case() to normalize in user input data?

How to use array_change_key_case() to normalize in user input data?

M66 2025-04-25

In PHP development, especially when it comes to data entered by users, we often encounter inconsistent case of key names entered by users. For example, the form data entered by the user or the key names in the URL parameters may have different case formats. At this time, if unified case processing is not performed, some unexpected errors may occur.

PHP provides a very convenient function array_change_key_case() , which can be used to change the case format of array key names. This function is very suitable for handling case inconsistencies in user input.

The function of array_change_key_case() function

The array_change_key_case() function is used to convert all key names of an array to the specified upper and lower case. You can choose to convert all key names of the array to lowercase or uppercase.

Function prototype:

 array_change_key_case(array $array, int $case = CASE_LOWER): array
  • $array : The array to be processed.

  • $case : Specifies the converted case. It can be CASE_UPPER or CASE_LOWER , the default value is CASE_LOWER , indicating that all key names are converted to lowercase.

Example: Unified case format

Suppose you receive the following form data from user input, the key names may have different case formats:

 $userInput = [
    'Name' => 'John',
    'AGE' => 25,
    'email' => 'john@example.com'
];

If you want to uniformly convert all key names to lowercase, you can use array_change_key_case() :

 <?php
$userInput = [
    'Name' => 'John',
    'AGE' => 25,
    'email' => 'john@example.com'
];

// Convert key names of arrays to lowercase
$normalizedInput = array_change_key_case($userInput, CASE_LOWER);

print_r($normalizedInput);
?>

The output will be:

 Array
(
    [name] => John
    [age] => 25
    [email] => john@example.com
)

As you can see, all key names are converted to lowercase, avoiding problems caused by case inconsistencies.

Process URL parameters

Sometimes, the parameters in the URL are also where we need to deal with them. For example, the URL may take query parameters with different cases, and we want to unify the case format of these parameters. Assume your URL is like this:

 https://m66.net/profile?UserName=JohnDoe&Email=john@example.com&Age=30

Use parse_url() and parse_str() to parse the URL and unify the upper and lower case of the parameters through array_change_key_case() :

 <?php
$url = "https://m66.net/profile?UserName=JohnDoe&Email=john@example.com&Age=30";

// AnalysisURL
$parsedUrl = parse_url($url);
parse_str($parsedUrl['query'], $queryParams);

// Convert the key name of the query parameter to lowercase
$normalizedParams = array_change_key_case($queryParams, CASE_LOWER);

print_r($normalizedParams);
?>

The output will be:

 Array
(
    [username] => JohnDoe
    [email] => john@example.com
    [age] => 30
)

In this way, you can ensure that even if the parameter key names in the URL are inconsistent, they will eventually be unified into the format you want.

Summarize

array_change_key_case() is a very useful function that can help you unify the case of array key names when processing arrays. When processing user input or URL parameters, it is very important to uniform case. It can reduce errors caused by case inconsistency and ensure consistency in data processing. Hopefully, with the examples in this article, you can better understand how to use array_change_key_case() to solve the real problem.