PHP array_change_key_case() Function With Example

Hello Friends, In this short article, I will discuss the php array_change_key_case() function with an example. When using array_change_key_case(), you have two primary case conversion options: converting keys to uppercase or lowercase.  It can convert all keys to uppercase, or lowercase, or capitalize the first character of each key. This can be particularly useful when you want to standardize the case of array keys for consistency or comparison purposes.

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

Parameters:

  • $input_array: The input array whose keys you want to change the case of.
  • $case: An optional parameter that specifies the case conversion.
    • CASE_UPPER (default): Convert keys to uppercase.
    • CASE_LOWER: Convert keys to lowercase.

Return Value:

  • An array with all the keys changed to the specified case.

Here’s an example of how to use the array_change_key_case function:

PHP array_change_key_case

Output:-

In this example, we first define an input array with mixed case keys. Then we use the array_change_key_case function to create two new arrays: one with lowercase keys and another with uppercase keys. The output demonstrates how the case of the keys is changed in each modified array while keeping the original data intact.

Leave a Comment