What is the array_change_key_case method in PHP?

The array_change_key_case method can change the case of all keys of the array to either upper or lower case keys. The numeric keys are left unchanged.

Syntax

array_change_key_case(array $array, int $case = CASE_LOWER): array

Arguments

  • array: The array in which the case of the keys is to be changed.

  • case: Denotes the resulting case of array keys. The possible values can be:

    • CASE_UPPER: Equivalent int value is 1.
    • CASE_LOWER: Equivalent int value is 0.

case is an optional value. The default value is CASE_LOWER.

Example

<?php
$arr = array(10, 20, "onE" => 1, "TwO" => 2, "THrEe" => 3);
echo "Converting to Uppercase\n";
print_r(array_change_key_case($arr, CASE_UPPER));
echo "Converting to Lowercase\n";
print_r(array_change_key_case($arr, CASE_LOWER));
?>

In the code above, we created an array and converted all the array’s keys to uppercase, and then tried to convert to lowercase using the array_change_key_case method.