Current Location: Home> Function Categories> array_change_key_case

array_change_key_case

Change the case of all keys in the array
Name:array_change_key_case
Category:Array
Programming Language:php
One-line Description:Change all keys in the array to lowercase or uppercase.

Definition and usage

array_change_key_case() function converts all keys of an array into uppercase or lowercase letters.

The numeric index of the array does not change. If no optional parameter is provided (i.e. the second parameter), the default conversion is to lowercase letters.

Example

Example 1

Convert all keys of the array to capital letters:

 <?php
$age = array ( "Bill" => "60" , "Steve" => "56" , "Mark" => "31" ) ;
print_r ( array_change_key_case ( $age , CASE_UPPER ) ) ;
?>

Try it yourself

Example 2

Convert all keys of the array to lowercase letters:

 <?php
$age = array ( "Bill" => "60" , "Steve" => "56" , "Mark" => "31" ) ;
print_r ( array_change_key_case ( $age , CASE_LOWER ) ) ;
?>

Try it yourself

Example 3

If two or more keys are equal after running array_change_key_case() (such as "b" and "B"), the last element overwrites the other elements:

 <?php
$pets = array ( "a" => "Cat" , "B" => "Dog" , "c" => "Horse" , "b" => "Bird" ) ;
print_r ( array_change_key_case ( $pets , CASE_UPPER ) ) ;
?>

Try it yourself

grammar

 array_change_key_case ( array , case ) ;
parameter describe
array Required. Specifies the array to be used.
case

Optional. Possible values:

  • CASE_LOWER - Default value. Converts the keys of the array to lowercase letters.
  • CASE_UPPER - Converts the keys of the array to uppercase letters.