How to Change Array Key to Lowercase in PHP ?
Last Updated :
10 Jul, 2024
This article will show you how to change the array key case to lowercase using PHP. There are two approaches to changing the key case, these are:
Using array_change_key_case() function
The array_change_key_case() function is used to change the case of all of the keys in a given array either to lower case or upper case. To change the key case to lowercase, we will add the value "CASE_LOWER".
Syntax:
array_change_key_case($arr, CASE_LOWER)
Example 1:
PHP
<?php
// PHP Program to change the key
// case to lovercase
function change_case($arr) {
return array_change_key_case($arr, CASE_LOWER);
}
// Driver Code
$arr = [
"NAME" => "Akash",
"COMPANY" => "GFG",
"Address" => "Noida",
"Contact NO" => "+91-9876543210",
];
print_r(change_case($arr));
?>
OutputArray
(
[name] => Akash
[company] => GFG
[address] => Noida
[contact no] => +91-9876543210
)
Using foreach() and strtolower() functions
In this approach, we will use foreach() loop to select each key one by one, and then use strtolower() function to convert the key to lowercase.
Example:
PHP
<?php
// PHP Program to change the key
// case to lovercase
function change_case($arr)
{
foreach ($arr as $key => $val) {
echo strtolower($key)
. " => " . $val . "\n";
}
}
// Driver Code
$arr = [
"NAME" => "Akash",
"COMPANY" => "GFG",
"Address" => "Noida",
"Contact NO" => "+91-9876543210",
];
print_r(change_case($arr));
?>
Outputname => Akash
company => GFG
address => Noida
contact no => +91-9876543210
Using array_walk() with array_flip()
In PHP, array_walk() applies a callback function to each element of an array. When combined with array_flip(), it flips the keys and values of the array. This technique is useful for manipulating keys, such as changing them to lowercase or performing key-based operations efficiently.
Example:
PHP
<?php
$array = ["Name" => "John", "Age" => 30, "Country" => "USA"];
// Flip the array, then walk through it to perform operations on keys or values
array_walk(array_flip($array), function(&$value, $key) {
// Modify the keys or values here
$value = strtoupper($value); // Example: Convert values to uppercase
});
print_r($array);
?>
OutputArray
(
[Name] => John
[Age] => 30
[Country] => USA
)
Using array_combine() and array_map() functions
In this approach, we first use array_keys() to extract the keys and array_map() with strtolower() to convert them to lowercase. Then, we use array_combine() to combine the new lowercase keys with the original values.
Example:
PHP
<?php
function change_key_case_lower($arr) {
$lowercase_keys = array_map('strtolower', array_keys($arr));
return array_combine($lowercase_keys, array_values($arr));
}
// Example usage:
$original_array = array("First" => 1, "Second" => 2, "Third" => 3);
$lowercase_array = change_key_case_lower($original_array);
print_r($lowercase_array);
?>
OutputArray
(
[first] => 1
[second] => 2
[third] => 3
)
Using array_reduce()
array_reduce() can be used to iterate over the array and build a new array with lowercase keys.
Example: In this example we converts the keys of an associative array to lowercase using array_reduce and array_keys, and then prints the modified array
PHP
<?php
$arr = ["First" => 1, "Second" => 2, "Third" => 3];
$lowercaseKeysArr = array_reduce(array_keys($arr),
function($carry, $key) use ($arr) {
$carry[strtolower($key)] = $arr[$key];
return $carry;
}, []);
print_r($lowercaseKeysArr);
?>
OutputArray
(
[first] => 1
[second] => 2
[third] => 3
)
Similar Reads
How to Encode Array in JSON PHP ? Encoding arrays into JSON format is a common task in PHP, especially when building APIs or handling AJAX requests. Below are the approaches to encode arrays into JSON using PHP: Table of Content Using json_encode()Encoding Associative ArraysCustom JSON SerializationUsing json_encode()PHP provides a
2 min read
How to reset Array in PHP ? You can reset array values or clear the values very easily in PHP. There are two methods to reset the array which are discussed further in this article. Methods: unset() Functionarray_diff() Function Method 1: unset() function: The unset() function is used to unset a specified variable or entire arr
2 min read
How to check foreach Loop Key Value in PHP ? In PHP, the foreach loop can be used to loop over an array of elements. It can be used in many ways such asTable of ContentUsing the for-each loop with simple valuesUsing the foreach loop with Key-Value pairsUsing array_keys Function to Access Keys and ValuesUsing array_walk Function for IterationUs
3 min read
PHP | array_change_key_case() Function The array_change_key_case() function is an inbuilt function in PHP and is used to change case of all of the keys in a given array either to lower case or upper case. Syntax: array array_change_key_case(in_array, convert_case) Parameters: This function accepts two parameters out of which one is manda
3 min read
How to get specific key value from array in PHP ? In this article, we will see how to get specific key values from the given array. PHP array is a collection of items that are stored under keys. There are two possible types of keys: strings and integers. For any type of key, there is a common syntax to get a specific value by key â square brackets.
3 min read
PHP array_âkey_âlast() Function The array_âkey_âlast() function is an inbuilt function in PHP that is used to get the last key of an array. This function returns the last key without affecting the internal array pointer. Syntax: int|string|null array_âkey_âlast(array $array) Parameters: This function accepts single parameter $arra
1 min read
Sort an Associative Array by Key in PHP Given an Associative Array, the task is to sort the associative array by its keys in PHP. There are different methods to sort Associative Array by keys, these are described below: Table of ContentUsing ksort() FunctionUsing uksort() FunctionConverting to a Regular Array for SortingUsing array_multis
3 min read
How to Loop Through an Array using a foreach Loop in PHP? Given an array (indexed or associative), the task is to loop through the array using foreach loop. The foreach loop iterates through each array element and performs the operations. PHP foreach LoopThe foreach loop iterates over each key/value pair in an array. This loop is mainly useful for iteratin
2 min read
How to get elements in reverse order of an array in PHP ? An array is a collection of elements stored together. Every element in an array belong to a similar data type. The elements in the array are recognized by their index values. The elements can be subjected to a variety of operations, including reversal. There are various ways to reverse the elements
4 min read
PHP Change strings in an array to uppercase Changing strings in an array to uppercase means converting all the string elements within the array to their uppercase equivalents. This transformation modifies the array so that every string, regardless of its original case, becomes fully capitalized.Examples:Input : arr[] = ("geeks", "For", "GEEks
3 min read