![]() |
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: Table of Content Using array_change_key_case() functionThe 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:
Output Array ( [name] => Akash [company] => GFG [address] => Noida [contact no] => +91-9876543210 ) Using foreach() and strtolower() functionsIn 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:
Output name => 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:
Output Array ( [Name] => John [Age] => 30 [Country] => USA ) Using array_combine() and array_map() functionsIn 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:
Output Array ( [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
Output Array ( [first] => 1 [second] => 2 [third] => 3 ) |
Reffered: https://www.geeksforgeeks.org
Geeks Premier League |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 11 |