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
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.
|
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.
1 2 3 4 5 6 7 8 9 10 |
<?php //first way of declaring associative array $myArr = array("Cycle" => 2, "Bike" => 5, "Car" => 9); //Convert array key to uppercase $myUcaseArr = array_change_key_case($myArr, CASE_UPPER); //Print result print_r($myUcaseArr); ?> |
Output
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.
1 2 3 4 5 6 7 8 9 10 |
<?php //first way of declaring associative array $myArr = array("Cycle" => 2, "Bike" => 5, "Car" => 9); //Convert array key to uppercase $myUcaseArr = array_change_key_case($myArr, CASE_UPPER); //Print result print_r($myUcaseArr); ?> |
Output
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