PHP Function Reference

PHP array_change_key_case() Function



The PHP array_change_key_case() function changes the case of all keys in an array. It returns an array with all keys of the given array in either lowercase or uppercase. Numbered indices remains unchanged.

Syntax

array_change_key_case(array, case)

Parameters

array Required. Specify the array to work on.
case Optional. Specify one of the following:
  • CASE_LOWER : Default - Changes all keys of the array in lowercase.
  • CASE_UPPER : Changes all keys of the array in uppercase.

Return Value

Returns an array with its keys in either lowercase or uppercase, or null if array is not an array.

Exceptions

Throws E_WARNING if array is not an array.

Example:

The example below shows the usage of array_change_key_case() function.

<?php
$Arr = array("Red" => 1, 
             "Green" => 2, 
             "Blue" => 3);

//creating an array with keys in lowercase
$lc_Arr = array_change_key_case($Arr, CASE_LOWER);

//creating an array with keys in lowercase
$uc_Arr = array_change_key_case($Arr, CASE_UPPER);

print_r($Arr);
echo "\n";
print_r($lc_Arr);
echo "\n";
print_r($uc_Arr);
?>

The output of the above code will be:

Array
(
    [Red] => 1
    [Green] => 2
    [Blue] => 3
)

Array
(
    [red] => 1
    [green] => 2
    [blue] => 3
)

Array
(
    [RED] => 1
    [GREEN] => 2
    [BLUE] => 3
)

❮ PHP Array Reference