PHP array_change_key_case() Function with Example

PHP array_change_key_case() Function: Here, we will find out about the array_change_key_case() work with example in PHP.

PHP array_change_key_case() Function

array_change_key_case() work is an exhibit based capacity, which is utilized to change the case (lowercase or capitalized) of all keys in a cluster.

As we realize that an exhibit may contain keys and values, by utilizing this capacity, we can change the instance of the keys.

Syntax:

array_change_key_case(array_name, [case_value]) : exhibit

here,

  • array_name is the name of the exhibit where we need to change the instance of the keys.
  • case_value is a discretionary parameter, it has two values. CASE_LOWER – which changes over all keys in lowercase and CASE_UPPER – which changes over all keys in capitalized. Its default value is “CASE_LOWER“. In the event that we don’t utilize this parameter, all keys of the cluster convert into lowercase.

It restores an exhibit with the keys which are changed over either in lowercase or capitalized.

Example:

Input:
$arr = array("Name" => "Aakash", "City" => "Bilaspur");
Function call: 
array_change_key_case($arr)
Output:
Array
(
    [name] => Aakash
    [city] => Bilaspur
)

Input:
$arr = array("Name" => "Aakash", "City" => "Bilaspur");
Function call: 
array_change_key_case($arr, CASE_UPPER)
Output:
Array
(
    [NAME] => Aakash
    [CITY] => Bilaspur
)

PHP Code:

<?php
	$arr = array("Name" => "Aakash", "City" => "Bilaspur");
	print ("array before changing key case ...\n");
	print_r ($arr);
	
	print ("array after chaging key case (default case)...\n");
	//default case will be lower
	print_r (array_change_key_case($arr));

	print ("array after chaging key case (lowercase)...\n");
	//defining lowercase
	print_r (array_change_key_case($arr, CASE_LOWER));	
	
	print ("array after chaging key case (uppercase)...\n");
	//defining uppercase
	print_r (array_change_key_case($arr, CASE_UPPER));	
?>

Output:

array before changing key case ...
Array
(
    [Name] => Aakash
    [City] => Bilaspur
)
array after chaging key case (default case)...
Array
(
    [name] => Aakash
    [city] => Bilaspur
)
array after chaging key case (lowercase)...
Array
(
    [name] => Aakash
    [city] => Bilaspur
)
array after chaging key case (uppercase)...
Array
(
    [NAME] => Aakash
    [CITY] => Bilaspur
)

Leave a Comment

error: Alert: Content is protected!!