Current Location: Home> Latest Articles> The effect of array_change_key_case() used before or after array_unique()

The effect of array_change_key_case() used before or after array_unique()

M66 2025-05-12

When processing arrays in PHP, the order of function calls often has a critical impact on the results. array_change_key_case() and array_unique() are two functions that are commonly used for array operations, but have you ever thought that when these two functions are used together, their order will affect the result? This article will take you to understand their usage and order differences in order.

Function introduction

array_change_key_case(array $array, int $case = CASE_LOWER): array

This function is used to uniformly convert the key names of an array to lowercase ( CASE_LOWER ) or uppercase ( CASE_UPPER ). This is a very useful tool when dealing with inconsistent key names (such as parameters entered by the user).

 $data = [
    "Name" => "Alice",
    "AGE" => 25,
    "Gender" => "Female"
];

$result = array_change_key_case($data, CASE_LOWER);
// result:['name' => 'Alice', 'age' => 25, 'gender' => 'Female']

array_unique(array $array, int $flags = SORT_STRING): array

This function is used to remove duplicate values ​​from the array, retaining only the first key-value pair that appears. It should be noted that it only acts on the "value", not the key name.

 $data = ["apple", "banana", "Apple", "banana"];
$result = array_unique($data);
// Default is case sensitive:['apple', 'banana', 'Apple']

Differential analysis of order of use

Let's combine these two functions to see how the results will change if the order is different.

1. array_change_key_case() is placed in front

 $data = [
    "Name" => "Alice",
    "name" => "Bob",
    "AGE" => 25,
    "age" => 30
];

$data = array_change_key_case($data, CASE_LOWER);
$data = array_unique($data);

print_r($data);

Output result:

 Array
(
    [name] => Alice
    [age] => 25
)

In this case, the case of the key name is unified first, and "Name" and "name" are treated as the same key, so the latter is overwritten. The repetitions on the value are removed using array_unique() , but the impact is not great because the keys are already unified.

2. Array_unique() is placed in front

 $data = [
    "Name" => "Alice",
    "name" => "Bob",
    "AGE" => 25,
    "age" => 30
];

$data = array_unique($data);
$data = array_change_key_case($data, CASE_LOWER);

print_r($data);

Output result:

 Array
(
    [name] => Alice
    [age] => 25
    [age_1] => 30
)

In this way, the key-value pair with the unique value is retained first, and then the key name is unified. As a result, there will be key name conflicts, such as "AGE" and "age" are both preserved, but the key names are repeated after being converted to lowercase. PHP will automatically handle this conflict in the array (such as tagging with temporary key names such as age_1 ).

Which order is more reasonable?

It depends on your intention to process the data:

  • If you pay more attention to the consistency of key names (for example, to use key names to retrieve values), you should first use array_change_key_case() .

  • If you focus more on removing duplicate values ​​and the key names are not important to you, the order will not have much impact, but it is recommended to array_unique() first to preserve more original data structures.

Practical application examples

Suppose you receive user-submitted data from a form, with inconsistent case keys and duplicate values, you want to unify them and send them to the API interface https://m66.net/api/submit.php :