PHP array_change_key_case() Function

What is PHP array_change_key_case() Function?

When you want to transform case of all the keys of an array either to uppercase or to lowercase, use array_change_key_case() function.

Syntax:

array_change_key_case(array, case)

Parameters:

The Function has 1 required parameter and 1 optional parameter-

array (Required): This is an array that we will work on.

case (Optional): It is the case which is either lowercase or uppercase. It decides what case we will convert the array keys to. It two possible values-

  • CASE_LOWER: This converts all the array keys to lower case (. Check example 1). It is the optional value which mean if you don’t mention this parameter the function will convert all the keys to lower case. Check example 2.
  • CASE_UPPER: This converts all the array keys to upper case. Check example 3.

Return Values:

The function returns a new array with converted key case which is specified in the second parameter.

Examples:

Example 1:

<pre>
<?php
$language = array(
    "A" => "Assembly",
    "b" => "Basic",
    "d" => "Dart",
    "p" => "PHP"
);
print_r(array_change_key_case($language, CASE_LOWER));
?>
</pre>

Output:

Array
(
    [a] => Assembly
    [b] => Basic
    [d] => Dart
    [p] => PHP
)

Explanation:

The CASE_LOWER parameter converts cases of all the keys to lower case.

Example 2:

<pre>
<?php
$language = array(
    "A" => "Assembly",
    "b" => "Basic",
    "d" => "Dart",
    "p" => "PHP"
);
print_r(array_change_key_case($language));
?>
</pre>

Output:

Array
(
    [a] => Assembly
    [b] => Basic
    [d] => Dart
    [p] => PHP
)

Explanation:

As there is no second parameter, the function considers it as CASE_LOWER and converts cases of all the keys to lower case.

Example 3:

<pre>
<?php
$language = array(
    "a" => "Assembly",
    "b" => "Basic",
    "d" => "Dart",
    "J" => "JavaScript"
    "p" => "PHP"
);
print_r(array_change_key_case($language,CASE_UPPER));
?>
</pre>

Output:

Array
(
    [A] => Assembly
    [B] => Basic
    [C] => CSS
  [J] => JavaScript
    [P] => PHP
)

Explanation:

The CASE_UPPER parameter converts cases of all the keys to upper case.

Example 4:

<pre>
<?php
$language = array(
    "P" => "Assembly",
    "p" => "Basic"
);
print_r($language);
?>
</pre>

Output:

Array
(
    [P] => Assembly
    [p] => Basic
)

Explanation:

Though both keys are same as “p” but they are different in case, so the it is considered two different elements of an array.

Example 5:

<pre>
<?php
$language = array(
    "a" => "Assembly",
    "b" => "Basic",
    "d" => "Dart",
    "J" => "Java",
    "j" => "JavaScript"
);
print_r(array_change_key_case($language, CASE_UPPER));
?>
</pre>

Output:

Array
(
    [A] => Assembly
    [B] => Basic
    [D] => Dart
    [J] => JavaScript
)

Explanation:

keys of the last two elements are same “j”, though both have different cases. The CASE_UPPER parameter transforms all the keys to uppercase and the last element (JavaScript) replaces the previous one (Java).

Example 6:

<pre>
<?php // #6
$language = array(
    "a" => "Assembly",
    "b" => "Basic",
    "scripting" => array(
        "j" => "JavaScript",
        "v" => "VBScript"
    )
);
print_r(array_change_key_case($language, CASE_UPPER));
?>
</pre>

Output:

Array
(
    [A] => Assembly
    [B] => Basic
    [SCRIPTING] => Array
        (
            [j] => JavaScript

            [v] => VBScript
        )
)

Practical Usages of array_change_key_case() Function:

An array may have mixed case keys that you want to standardize converting all to either upper or lower case. When you want to keep case of the array keys consistence, use this function.

Notes on array_change_key_case() Function:

  • Keys with same values but different cases considered different indices. Check example 4.
  • The function has no effect on an indexed array (numeric keys). So, there is no change in an array that has numbered indices.
  • If the input array has multiple indices with same key cases or different key cases, the last element will override the previous elements after the case transformation. Check example 5.
  • The function can’t change cases of the inner level keys of a multidimensional array. It can only transform keys of the outer most level keys. Check example 6.
  • Trying to use a variable with any data type other than array shows a fatal error.

PHP Version Support:

PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8

Summary: PHP array_change_key_case() Function

When you need to convert all the array keys from lower to upper case to vice versa, array_change_key_case() allows you to accomplish it quickly. It is one of the many built-in PHP array functions.

Reference:

https://www.php.net/manual/en/function.array-change-key-case.php