When processing arrays in PHP, array_change_key_case() and array_merge() are both very commonly used functions. One can convert the keys of an array to case uniformly, and the other is used to merge arrays. Both seem to be simple and straightforward, but when used together , it is easy to get stuck, especially when dealing with associative arrays .
array_change_key_case(array $array, int $case = CASE_LOWER) : Converts all key names of the array to lowercase or uppercase.
array_merge(array ...$arrays) : Merge one or more arrays.
The key names of arrays in PHP will show different "sensitivity" in some functions - for example, array_merge() is case-sensitive, while array_change_key_case() will uniformly case. This may lead to problems such as "duplicate key not overwritten" or "unexpected overwritten" during merge.
$arr1 = ['Name' => 'Alice'];
$arr2 = ['name' => 'Bob'];
$result = array_merge(
array_change_key_case($arr1, CASE_LOWER),
array_change_key_case($arr2, CASE_LOWER)
);
print_r($result);
Array
(
[name] => Bob
)
It looks normal, right? Because we are all unified in lowercase. But if you change the order:
$result = array_merge(
array_change_key_case($arr2, CASE_LOWER),
array_change_key_case($arr1, CASE_LOWER)
);
Array
(
[name] => Alice
)
Note: The values covered are different!
This shows that when you are using array_merge() and array_change_key_case() , the processing order will directly affect the result.
$arr1 = ['Name' => 'Alice'];
$arr2 = ['name' => 'Bob'];
$result = array_change_key_case(
array_merge($arr1, $arr2), CASE_LOWER
);
print_r($result);
Array
(
[name] => Bob
)
In this example, although array_merge() regards Name and name as two different keys during merge, and only becomes lowercase after merge, so that the latter Bob is retained, that is, "it seems to be repeated, but in fact both keys exist when merging."
And your original intention is to merge based on a certain key, and this writing may cause unexpected behavior.
To avoid these pitfalls, it is recommended to follow the following rules when using these two functions:
First unify the upper and lower case of the key names and then merge:
$result = array_merge(
array_change_key_case($arr1, CASE_LOWER),
array_change_key_case($arr2, CASE_LOWER)
);
Ensure that the case style of keys in the data source is consistent: it is best to unify the case standards during the data generation or source stage.
Consider using array_replace() instead of array_merge(): In some scenarios, array_replace() is more in line with the logic of "key override".
$result = array_replace(
array_change_key_case($arr1, CASE_LOWER),
array_change_key_case($arr2, CASE_LOWER)
);
array_merge() is case sensitive ;
array_change_key_case() is used to unify the upper and lower case of key names ;
When the two are used in combination, the order of sequence and the original case of the keys will affect the final result;
In actual development, remember to do case conversion before merging to avoid "accidental loss" of data.
This kind of pitfall is particularly prone to occur in application scenarios of multi-system or multi-source data merging, such as interface aggregation (such as https://api.m66.net/userinfo and https://api.m66.net/userdetail ). If you don't handle case in advance, you may "overwrite" the key fields without knowing it.
I hope this article can help you avoid pitfalls!