In PHP, the case of array keys is very important because arrays are case sensitive. Sometimes we want to be able to unify the format of all keys in an array, such as converting all key names to lowercase or uppercase letters. array_change_key_case() is a useful function provided by PHP, which allows us to change the case of array keys.
However, there are some cases where we may want to have finer granular controls, such as converting some parts of the key name to lowercase and others to uppercase. To achieve this, we can use array_change_key_case() in conjunction with strtolower() and strtoupper() to customize the conversion rules for key names.
array_change_key_case() can be used to convert the case of array keys to uniform lowercase or uppercase. It accepts two parameters:
The first parameter is the array to be converted.
The second parameter is the way to convert, CASE_UPPER converts all keys to uppercase, and CASE_LOWER converts all keys to lowercase.
<?php
$array = [
"FirstName" => "John",
"LastName" => "Doe",
"Email" => "john.doe@m66.net"
];
$lowercaseArray = array_change_key_case($array, CASE_LOWER);
print_r($lowercaseArray);
?>
Output result:
Array
(
[firstname] => John
[lastname] => Doe
[email] => john.doe@m66.net
)
As shown above, all key names are converted to lowercase.
<?php
$array = [
"firstName" => "John",
"lastName" => "Doe",
"email" => "john.doe@m66.net"
];
$uppercaseArray = array_change_key_case($array, CASE_UPPER);
print_r($uppercaseArray);
?>
Output result:
Array
(
[FIRSTNAME] => John
[LASTNAME] => Doe
[EMAIL] => john.doe@m66.net
)
In this example, all key names are converted to uppercase.
Sometimes, we don't just want to convert all key names to lowercase or uppercase, but we want to convert some parts to lowercase and others to uppercase according to our needs. For example, we may want to convert "FirstName" to "first_name" and "LastName" to "LAST_NAME".
At this time, we can use strtolower() or strtoupper() to further adjust each key name after array_change_key_case() .
Suppose we want to convert the first letter of each word of the key name to uppercase and connect it with underscore, while converting the other letters to lowercase. We can do it by:
<?php
$array = [
"FirstName" => "John",
"LastName" => "Doe",
"Email" => "john.doe@m66.net"
];
function customKeyFormat($key) {
// Capitalize the initial letter of each word,Other letters lowercase,Connect with underscore
return strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $key));
}
$newArray = [];
foreach ($array as $key => $value) {
$newArray[customKeyFormat($key)] = $value;
}
print_r($newArray);
?>
Output result:
Array
(
[first_name] => John
[last_name] => Doe
[email] => john.doe@m66.net
)
In this example, we used the preg_replace() function to convert the camel nomenclature to lowercase and separated by underscore. Through strtolower(), it is further ensured that all letters are lowercase.
By combining array_change_key_case() , strtolower() and strtoupper() , we can flexibly handle format conversion of key names in PHP arrays. Whether it is converting all key names to uniform lowercase or uppercase, or implementing more complex custom formats, PHP provides enough tools to accomplish this task. This can help us operate key names more conveniently and uniformly when processing array data.
Hopefully, this article will help you better understand how to implement custom conversion of key name formats by combining these PHP functions.