PHP array_change_key_case() function

Example

Convert all the keys of the array to uppercase:

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

Run Instance

Definition and Usage

The array_change_key_case() function converts all the keys of an array to uppercase or lowercase.

The numerical indices of the array do not change. If the optional parameter is not provided (i.e., the second parameter), it defaults to converting to lowercase letters.

Tips and Comments

Note:If two or more keys are the same when running the function, the last element will overwrite the other elements (see Example 2).

Syntax

array_change_key_case(array,case);
Parameters Description
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.

Technical Details

Return Value: Returns an array with keys in uppercase or lowercase, or if array Returns FALSE if it is not an array.
PHP Version: 4.2+

More Examples

Example 1

Convert all the 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));
?>

Run Instance

Example 2

If there are two or more keys that are equal after running array_change_key_case() (for example, "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));
?>

Run Instance