PHP array_change_key_case() Function

Last Updated on August 12, 2021 by Roshan Parihar

PHP array_change_key_case() Function is used to change all the keys of an array to uppercase or lowercase. The values of an array remain the same after using the function.

Syntax of PHP array_change_key_case() Function

array_change_key_case(array, case)

The function takes two arguments in which the first argument is an array. The second argument is to specify whether you have to change the key to lowercase or uppercase that you will find below.

Description of Parameters

Name Description
array Required. Specify the array that you have to use to covert its keys to lowercase and uppercase.
case optional. Specify the case whether you want to convert to lowercase or uppercase.

  • CASE_LOWER: Change the keys to lowercase. It is a default value for the function.
  • CASE_UPPER: Convert the keys to uppercase.

Let’s see some examples that are useful to understand the use of the PHP array_change_key_case() Function in PHP.

Examples of PHP array_change_key_case() Function

See the example given below with a description to learn the use of the function.

Example 1: Convert Array Key to Lowercase

To convert the all keys of an array to lowercase, you have to use the keyword CASE_LOWER as the second argument of the function. However, it is the default value for the function, and don’t need to use it when you need to change the keys to lowercase.

Output

Array ( [cycle] => 2 [bike] => 5 [car] => 9 )

The above example contains an associative array with alphabetical keys. The PHP array_change_key_case() Function change all the keys to lowercase.

Example 2: Change Array Key to Uppercase

When you have to change all the keys of the given array to uppercase, you have to use the keyword CASE_UPPER as the second argument of the function.

Output

Array ( [CYCLE] => 2 [BIKE] => 5 [CAR] => 9 )

The output in the above example shows the keys that are changed to uppercase in PHP.

The function is useful to convert the keys from lowercase to uppercase and from uppercase to lowercase.

You May Also Like to Read

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.