| by Arround The Web | No comments

PHP Array_Change_Key_Case() Function

If you want to convert your array keys into the uppercase or lowercase in PHP, array_change_key_case() is used to convert the keys in an array into the Upper case or Lower case.

Let’s see the following syntax:
array_change_key_case(array_input,CASE_LOWER/CASE_UPPER)

It takes two parameters.

Parameters:

  1. Array_input is the input array that has keys and values.
  2. The second parameter specifies the case in which keys are converted. It takes two possible values:

CASE_LOWER converts all the keys present in the array to lower.

CASE_UPPER converts all the keys present in the array to upper.

By default, the keys are converted into lowercase.

Array holds the data in a linear fashion. It holds multiple elements at a time. Array() is used to create an array in PHP. A key can refer to a value by using the => operator.

Syntax:
array(Key=>Value,……..)

To display the entire array, we can use the print_r() function. It returns the array in the format such that the key is placed inside the [] followed by the value.

Array

(
[Key] => Value
……………
……………
)

Let’s create an array with four keys and values.

<?php
//create an array named - Flower with four keys and values
$Flower=array("Flower_name"=>"Lotus","Flower_area"=>"water","Flower_sepals"=>10,"Flower_petals"=>20);

//display the Flower array
print_r($Flower);
?>

Output:

You can see that the keys are “Flower_name”, “Flower_area”, “Flower_sepals, and “Flower_petals”.

Now, we convert them into the upper/lower case using the function.

Example 1:

In this example, we will use the array_change_key_case() function to convert the keys in the array to lowercase without specifying a second parameter.

<?php
//create an array named - Flower with four keys and values
$Flower=array("Flower_name"=>"Lotus","Flower_area"=>"water","Flower_sepals"=>10,"Flower_petals"=>20);

//display the Flower array by converting the keys into lowercase
print_r(array_change_key_case($Flower));
?>

Output:

You can observe that all the four keys in the array are converted to lowercase.

Example 2:

In this example, we will use the array_change_key_case() function to convert the keys in the array to lowercase by specifying a second parameter – CASE_LOWER.

<?php
//create an array named - Flower with four keys and values
$Flower=array("Flower_name"=>"Lotus","Flower_area"=>"water","Flower_sepals"=>10,"Flower_petals"=>20);

//display the Flower array by converting the keys into lowercase
print_r(array_change_key_case($Flower,CASE_LOWER));
?>

Output:

You can observe that all the four keys in the array are converted to lowercase.

Example 3:

In this example, we will use the array_change_key_case() function to convert the keys in the array to uppercase by specifying a second parameter – CASE_UPPER.

<?php
//create an array named - Flower with four keys and values
$Flower=array("Flower_name"=>"Lotus","Flower_area"=>"water","Flower_sepals"=>10,"Flower_petals"=>20);

//display the Flower array by converting the keys into uppercase
print_r(array_change_key_case($Flower,CASE_UPPER));
?>

Output:

You can observe that all the four keys in the array are converted to uppercase.

Conclusion

From this PHP tutorial, we learned how to convert all the keys in an array to lower/upper case using the array_key_case() function. It is a built-in function that takes the CASE_LOWER as a second parameter to convert the keys to lowercase and takes the CASE_UPPER to convert the keys to uppercase. If we don’t specify the second parameter, it converts into lowercase.

Share Button

Source: linuxhint.com

Leave a Reply