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

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

M66 2025-04-24

When processing arrays in PHP, two very practical functions are often used: array_change_key_case() and array_filter() . These two functions are used to change the case of array keys and filter elements in the array respectively. Many developers may ask when using these two functions: Is the order of call of these two functions important? Will it affect the final processing results?

This article will answer this question through analysis and examples.

One, two functions introduction

1. array_change_key_case()

This function is used to convert the key names of an array to all lowercase or all uppercase, and is used as follows:

 array_change_key_case(array $array, int $case = CASE_LOWER): array
  • $case can be CASE_LOWER (default) or CASE_UPPER .

Example:

 $arr = ['Name' => 'Tom', 'Age' => 25];
print_r(array_change_key_case($arr, CASE_LOWER));
// Output:Array ( [name] => Tom [age] => 25 )

2. array_filter()

This function is used to filter elements in an array, and you can customize the callback function to decide which elements should be retained.

 array_filter(array $array, ?callable $callback = null, int $mode = 0): array

By default, it removes elements with "false values" such as false , null , 0 , empty strings, etc.

Example:

 $arr = ['name' => 'Tom', 'age' => 0, 'email' => ''];
print_r(array_filter($arr));
// Output:Array ( [name] => Tom )

2. Different orders, are the results different?

Yes, the order may affect the results , depending on the specific array structure and purpose you are dealing with.

Case 1: array_change_key_case() is called after array_filter()

In this case, the key case is converted first, and then the value is filtered.

 $data = ['Name' => 'Tom', 'Age' => null, 'EMAIL' => ''];
$result = array_filter(array_change_key_case($data));
print_r($result);

Output:

 Array ( [name] => Tom )

illustrate:

  • Keys are all lowercase: name , age , email

  • null and empty strings are removed

Case 2: After array_filter(), array_change_key_case() is called

 $data = ['Name' => 'Tom', 'Age' => null, 'EMAIL' => ''];
$result = array_change_key_case(array_filter($data));
print_r($result);

Output:

 Array ( [name] => Tom )

It looks like the output is the same, but when you use array_filter() with the callback function, the difference may come out.

More complex examples:

 $data = ['Name' => 'Tom', 'Age' => 0, 'EMAIL' => 'tom@m66.net'];

$result1 = array_filter(array_change_key_case($data), function($val) {
    return !empty($val);
});

$result2 = array_change_key_case(array_filter($data, function($val) {
    return !empty($val);
}));

print_r($result1);
print_r($result2);

Output:

 $result1:
Array ( [name] => Tom [email] => tom@m66.net )

$result2:
Array ( [name] => Tom [email] => tom@m66.net )

The results are still the same at this time, but when you need to do further logical processing based on the upper and lower case of the key name , the order may affect your judgment logic.

3. How to choose the order in actual development?

It is recommended to judge based on needs:

  • If you want to uniformly process the format of the key (such as converting all lowercase to avoid case confusion), you can use array_change_key_case() first.

  • If you pay more attention to the filtering logic of array values ​​and don't care about the format of key names, it doesn't matter if you use array_filter() first.

  • When both must be used, it is best to clearly define the order of sequence to avoid uncertain logical errors.