The array_change_key_case() function is an inbuilt function in PHP and is used to change case of all of the keys in a given array either to lower case or upper case.
Syntax:
array array_change_key_case(in_array, convert_case)
Parameters: This function accepts two parameters out of which one is mandatory and the other is optional. The two parameters are described below:
- in_array (mandatory): This parameter refers to the array whose key’s case is needed to be changed.
- convert_case (optional): This is an optional parameter and refers to the ‘case’ in which we need to convert the keys of the array. This can take two values, either CASE_UPPER or CASE_LOWER. CASE_UPPER value determines the uppercase and CASE_LOWER determines lowercase. If the convert_case parameter is not passed then it’s default value is taken which is CASE_LOWER.
Note: If the second parameter is ignored then by default the keys of array will get converted to lowercase.
Return Type: The function returns an array with the changed case of the key, either to lowercase or to upper case.
Let us now look at some programs to get a better understanding of working of array_change_key_case() function.
-
Below program converts the case of keys to uppercase:
<?php// PHP code to illustrate array_change_key_case()// Both the parameters are passedfunctionchange_case($in_array){return(array_change_key_case($in_array, CASE_UPPER));}// Driver Code$array=array("Aakash"=> 90,"RagHav"=> 80,"SiTa"=> 95,"rohan"=> 85,"RISHAV"=> 70);print_r(change_case($array));?>chevron_rightfilter_noneOutput:
Array ( [AAKASH] => 90 [RAGHAV] => 80 [SITA] => 95 [ROHAN] => 85 [RISHAV] => 70 ) -
If we ignore the second parameter convert_case in the function array_change_key_case() then the keys will be converted to lowercase. Below program illustrates this:
<?php// PHP code to illustrate array_change_key_case()// Second parameter is ignoredfunctionchange_case($in_array){return(array_change_key_case($in_array));}// Driver Code$array=array("Aakash"=> 90,"RagHav"=> 80,"SiTa"=> 95,"rohan"=> 85,"RISHAV"=> 70);print_r(change_case($array));?>chevron_rightfilter_noneOutput:
Array ( [aakash] => 90 [raghav] => 80 [sita] => 95 [rohan] => 85 [rishav] => 70 ) -
If we don’t pass an array to the function then PHP_Warning is popped up, but the program works and No Output is generated. Below program illustrates this
<?php// PHP code to illustrate array_change_key_case()// NO parameter is passedfunctionchange_case($in_array){return(array_change_key_case());}// Driver Code$array=array("Aakash"=> 90,"RagHav"=> 80,"SiTa"=> 95,"rohan"=> 85,"RISHAV"=> 70);print_r(change_case($array));?>chevron_rightfilter_noneOutput:
No Output
Warning:
PHP Warning: array_change_key_case() expects at least 1 parameter, 0 given in /home/7d540b2d77cbbfa46af4fb8798fb5e79.php on line 5
Recommended Posts:
- How to get the function name inside a function in PHP ?
- PHP | Get PHP configuration information using phpinfo()
- PHP | php.ini File Configuration
- How to import config.php file in a PHP script ?
- PHP | imagecreatetruecolor() Function
- PHP | ImagickDraw getTextAlignment() Function
- PHP | Ds\Sequence last() Function
- Function to escape regex patterns before applied in PHP
- PHP | array_udiff_uassoc() Function
- PHP | geoip_continent_code_by_name() Function
- PHP | GmagickPixel setcolor() function
- PHP | opendir() Function
- PHP | cal_to_jd() Function
- PHP | stream_get_transports() Function
- PHP | Ds\Deque pop() Function
- PHP | SimpleXMLElement children() Function
- PHP | array_intersect_ukey() Function
- PHP | is_numeric() Function
- PHP | Imagick adaptiveSharpenImage() Function
- PHP | XMLWriter endDtdEntity() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

