Use array_change_key_case() array function in PHP 7.2, PHP 7.3 & 7.4 With Example

The `array_change_key_case()` function in PHP 7.2, PHP 7.3 & PHP 7.4 is used to change all keys in an array to either lowercase or uppercase. By default, it changes the keys to lowercase.

Syntax:

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

$array: The array you want to change the keys of.
$case: Optional parameter. Can be either `CASE_LOWER` (default) for lowercase or `CASE_UPPER` for uppercase.

Example 1: Convert Keys to Lowercase

<?php
// Original array with mixed case keys
$input_array = array("FirSt" => 1, "SecONd" => 2, "THIRD" => 3);
// Convert all keys to lowercase
$result = array_change_key_case($input_array, CASE_LOWER);
print_r($result);
?>

Output:

Array
(
[first] => 1
[second] => 2
[third] => 3
)

Example 2: Convert Keys to Uppercase

<?php
// Original array with mixed case keys
$input_array = array("FirSt" => 1, "SecONd" => 2, "THIRD" => 3);
// Convert all keys to uppercase
$result = array_change_key_case($input_array, CASE_UPPER);
print_r($result);
?>

Output:

Array
(
[FIRST] => 1
[SECOND] => 2
[THIRD] => 3
)

Notes:

– The function only affects the keys of the array, not the values.
– If a key already exists in the array in the desired case (e.g., `first` and `FIRST`), the last encountered value will overwrite the previous one.