Home > Article > Backend Development > A detailed explanation of the array_change_key_case() function in PHP
array_change_key_case() Converts all keys of the array to uppercase letters:
<?php /* array_change_key_case() 返回其键均为大写或小写的数组。 array array_change_key_case(array input[,int case]) 参数描述:array是要转换键值的数组 case有两个选项:CASE_LOWER,默认选项,以小写字母返回数组的键 CASE_UPPER,以大写字母返回数组的键 */ $input_array = array('a'=>'Java', 'B'=>'Php', 'c'=>'C++', 'D'=>'C#'); print_r(array_change_key_case($input_array, CASE_LOWER)); print_r(array_change_key_case($input_array, CASE_UPPER)); //如果在运行该函数时两个或多个键相同,则最后的元素会覆盖其他元素,例如: $input_array = array('a'=>'Barcelona', 'B'=>'Madrid', 'c'=>'Manchester', 'b'=>'Milan'); print_r(array_change_key_case($input_array, CASE_LOWER)); ?>
Definition and usage
array_change_key_case() function converts all keys of the array to uppercase letters or lowercase letter.
Syntax
array_change_key_case(array,case);
Parameters
Description
array Required. Specifies the array to use.
case Optional. Possible values:
CASE_LOWER - Default value. Convert an array's keys to lowercase letters.
CASE_UPPER - Convert array keys to uppercase letters.
Technical Details
Return value:
Returns an array with keys with lowercase letters, or returns with uppercase letters An array of letters with keys, or FALSE if array is not an array.
PHP version:
4.2+
Convert all keys of the array to lowercase letters:
<?php $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); print_r(array_change_key_case($age,CASE_LOWER)); ?>
If after running array_change_key_case() there are two or more keys with the same key (such as "b" and "B"), the last element will overwrite the other elements:
<?php $pets=array("a"=>"Cat","B"=>"Dog","c"=>"Horse","b"=>"Bird"); print_r(array_change_key_case($pets,CASE_UPPER)); ?>
More examples :
$cc = [ '0'=>[ 'Abc'=>'asdfasdf', 'BBAbc'=>'asdfasdf', 'AbDDc'=>'asdfasdf', ] ]; $tmp = array_change_key_case($cc); dump($tmp); return;
The printout result is
array (size=1) 0 => array (size=3) 'Abc' => string 'asdfasdf' (length=8) 'BBAbc' => string 'asdfasdf' (length=8) 'AbDDc' => string 'asdfasdf' (length=8)
The above is the detailed content of A detailed explanation of the array_change_key_case() function in PHP. For more information, please follow other related articles on the PHP Chinese website!