English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
array_change_key_case()The function changes the case of all keys in the passed array and returns an array with all keys in lowercase or uppercase depending on the option passed.
By default, this function returns an array with lowercase keys.
array array_change_key_case(array $input[, int $case])
Serial Number | Parameters and Description |
---|---|
1 | $input (required) This is the array you want to change the case of all keys. |
2 | $case (optional) This will take the constant valueCASE_UPPERorCASE_LOWER. If you do not pass this value, the function will change the keys to lowercase. |
The PHP array_change_key_case() function returns an array whose keys are in lowercase or uppercase form; if the input passed is not a valid PHP array, it returnsFALSE.
This function was originally introduced in PHP version4.2introduced in version 5.2.0.
Try the following example where we will convert all keys to uppercase-
<?php $input = array("FirSt"=> 10, "SecOnd" => 400, "Third" => 800, ); print_r(array_change_key_case($input, CASE_UPPER)); ?>Test to see‹/›
Output Result
Array ( [FIRST] => 10 [SECOND] => 400 [THIRD] => 800 )
The following example will convert all keys to lowercase-
<?php $input = array("FirSt"=> 10, "SecOnd" => 400, "Third" => 800, ); print_r(array_change_key_case($input, CASE_LOWER)); ?>Test to see‹/›
Output Result
Array ( [first] => 10 [second] => 400 [third] => 800 )
Let's check how it will work by default if we do not pass the second option in the function-
<?php $input = array("FirSt"=> 10, "SecOnd" => 400, "Third" => 800, ); print_r(array_change_key_case($input)); ?>Test to see‹/›
Output Result
Array ( [first] => 10 [second] => 400 [third] => 800 )
The following example returns FALSE and issues a warning because we are trying to pass a simple PHP string instead of a PHP array-
<?php $input = "This is a string"; print_r(array_change_key_case($input, CASE_LOWER)); ?>Test to see‹/›
This will not produce any output, but will display the following warning. If you want to check the return value of the function, it will be FALSE-
PHP Warning: array_change_key_case() expects parameter 1 to be array, string given in main.php on line 3