Home > Article > Backend Development > How to use array_change_key_case function in PHP to convert array key names to lowercase or uppercase
In PHP, an array is a very commonly used data type that can store multiple values, and each value can be accessed through a unique key. However, in the actual development process, the case of key names may be inconsistent. In this case, you need to use the PHP built-in function array_change_key_case to convert the case of key names.
array_change_key_case function can convert the key name of the array to lowercase or uppercase. This function accepts two parameters: the first parameter is the array to be converted, and the second parameter is an optional parameter indicating the type of conversion to uppercase and lowercase. The value of this parameter can be CASE_LOWER (convert to lowercase) or CASE_UPPER (convert to uppercase), default is CASE_LOWER.
The following is a sample code that uses the array_change_key_case function to convert the array key name to lowercase:
<?php $employee = array( "ID" => 101, "Name" => "Tom", "Age" => 25, "Gender" => "Male" ); $new_employee = array_change_key_case($employee, CASE_LOWER); print_r($new_employee); ?>
The output result is:
Array ( [id] => 101 [name] => Tom [age] => 25 [gender] => Male )
In the above sample code, we first create Create an array named $employee, which contains four key-value pairs. The key names are ID, Name, Age, and Gender. Then pass the array as the first parameter to the array_change_key_case function, and specify the second parameter as CASE_LOWER, so the function will convert the array key names to lowercase, and the new array will be saved in the $new_employee variable. Finally, we use the print_r function to output the converted array. You can see that the key names have all been changed to lowercase.
Similarly, if we want to convert the key names of the array to uppercase, we only need to set the second parameter value to CASE_UPPER. The sample code is as follows:
<?php $employee = array( "id" => 101, "name" => "Tom", "age" => 25, "gender" => "Male" ); $new_employee = array_change_key_case($employee, CASE_UPPER); print_r($new_employee); ?>
The output result is:
Array ( [id] => 101 [name] => Tom [age] => 25 [gender] => Male )
In this example, we first create an array named $employee and specify the array key names as lowercase. Then pass the array as the first parameter to the array_change_key_case function, and specify the second parameter as CASE_UPPER, so that the function will convert the key names of the array to uppercase, and the new array will be saved in the $new_employee variable. Finally, we use the print_r function to output the converted array. You can see that the key names have all been changed to uppercase.
In short, the array_change_key_case function is a very practical PHP built-in function that can help us quickly convert the key names of arrays to upper and lower case, thereby improving coding efficiency and code readability.
The above is the detailed content of How to use array_change_key_case function in PHP to convert array key names to lowercase or uppercase. For more information, please follow other related articles on the PHP Chinese website!