The PHP array_change_key_case is an inbuilt Function in PHP. It changes the keys of an array to upper or lowercase. In this article, we will discuss the PHP array_change_key_case Function. Also, we will discuss a few examples of using it.
Syntax
array array_change_key_case($array, $case = CASE_LOWER)
Parameters
The array_change_key_case Function expects two parameters. One parameter is mandatory while the second one is optional.
- $array: The first parameter refers to the input array. It is mandatory to pass it.
- $case: The second parameter is the case in which you need to convert keys. It is optional to pass and can have two possible values:
- CASE_UPPER: The function converts the array keys to uppercase.
- CASE_LOWER (default value): The function converts the array keys to lowercase. Also, it is the default value to the function.
Return Value
The PHP array_change_key_case Function returns an array after changing the case of the keys. However, by default, the function converts the keys to lowercase.
Note: The Function converts the cases of first level keys. In case of a multi dimensional array, the nested keys remain as it is.

Examples
Let’s discuss a few examples of using the function.
Example 1: Converting to Lower Case
By default, the array_change_key_case function in PHP converts the keys to lowercase.
<?php $testArray = array( 'Apple' => 1, 'BANANA' => 2, 'cat' => 3 ); $finalArray = array_change_key_case($testArray); print_r($finalArray); /* Array ( [apple] => 1 [banana] => 2 [cat] => 3 ) */ ?>
If we ignore the second parameter, the function converts all the keys to lowercase.
Example 2: Convert To Upper Case
You can convert the keys to Upper Case by passing the second parameter as CASE_UPPER.
<?php $testArray = array( 'Apple' => 1, 'BANANA' => 2, 'cat' => 3 ); $finalArray = array_change_key_case($testArray, CASE_UPPER); print_r($finalArray); /* Array ( [APPLE] => 1 [BANANA] => 2 [CAT] => 3 ) */ ?>
Example 3: Multi Dimensional Array
The function changes the case of only first level keys. Therefore, in a multi dimensional array, the nested keys remain the same.
<?php $testArray = array( 'Apple' => 1, 'BANANA' => 2, 'cat' => 3, 'dog' => array( 'abc' => 1, 'xyz' => 2 ) ); $finalArray = array_change_key_case($testArray, CASE_UPPER); print_r($finalArray); /* Array ( [APPLE] => 1 [BANANA] => 2 [CAT] => 3 [DOG] => Array ( [abc] => 1 [xyz] => 2 ) ) */ ?>
In the above example, notice that the first level keys (Apple, BANANA, dog) are converted to uppercase. However, the nested keys (abc and xyz) remain as it is.
Conclusion
In conclusion, we discussed the PHP array_change_key_case Function. You can read more about it on PHP Official Documentation. Also, you can read about other Array Functions on Concatly.

Vishesh is currently working as a Lead Software Engineer at Naukri.com. He passed out of Delhi College of Engineering in 2016 and likes to play Foosball. He loves traveling and is an exercise freak. His expertise includes Java, PHP, Python, Databases, Design and Architecture.