array_change_key_case() Function Use in PHP 8.1 & PHP 8.2 With Example

Support PHP Version: PHP 7.1, PHP 7.2, PHP 7.3, PHP 7.4, PHP 8.0, PHP 8.1, PHP 8.2, PHP 8.3 With Latest All Version Support.

The `array_change_key_case()` function in PHP is used to change the case of all keys in an array. You can change the keys to either lowercase or uppercase. This function is available in PHP 8.2 as well, and its usage remains the same.

Here is the syntax for `array_change_key_case()`:

<?php
array_change_key_case(array $array, int $case = CASE_LOWER): array
?>

1. `$array`: The input array.
2. `$case`: Can be `CASE_LOWER` (default) for lowercase conversion or `CASE_UPPER` for uppercase conversion.

Example

<?php
// Input array
$inputArray = [
"FirstName" => "John",
"LastName" => "Doe",
"Email" => "john.doe@example.com",
"Age" => 30
];

// Change all keys to lowercase
$lowercaseArray = array_change_key_case($inputArray, CASE_LOWER);
print_r($lowercaseArray);

// Change all keys to uppercase
$uppercaseArray = array_change_key_case($inputArray, CASE_UPPER);
print_r($uppercaseArray);
?>

Output

<?php
Array
(
[firstname] => John
[lastname] => Doe
[email] => john.doe@example.com
[age] => 30
)

Array
(
[FIRSTNAME] => John
[LASTNAME] => Doe
[EMAIL] => john.doe@example.com
[AGE] => 30
)
?>

In this example, the `array_change_key_case()` function is used twice:
1. To convert all the keys in `$inputArray` to lowercase.
2. To convert all the keys in `$inputArray` to uppercase.