Home » PHP

PHP array_change_key_case() Function with Example

PHP array_change_key_case() Function: Here, we are going to learn about the array_change_key_case() function with example in PHP.
Submitted by IncludeHelp, on February 07, 2019

PHP array_change_key_case() Function

array_change_key_case() function is an array based function, which is used to change the case (lowercase or uppercase) of all keys in an array.

As we know that an array may contain keys and values, by using this function, we can change the case of the keys.

Syntax:

    array_change_key_case(array_name, [case_value]) : array

Here,

  • array_name is the name the name of array in which we have to change the case of the keys.
  • case_value is an optional parameter, it has two values. CASE_LOWER – which converts all keys in lowercase and CASE_UPPER – which converts all keys in uppercase. Its default value is “CASE_LOWER”. If we do not use this parameter, all keys of the array convert into lowercase.

It returns an array with the keys which are converted either is lowercase or uppercase.

Examples:

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

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

PHP code:

<?php
	$arr = array("Name" => "Amit", "City" => "Gwalior");
	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] => Amit
    [City] => Gwalior
)
array after chaging key case (default case)...
Array
(
    [name] => Amit
    [city] => Gwalior
)
array after chaging key case (lowercase)...
Array
(
    [name] => Amit
    [city] => Gwalior
)
array after chaging key case (uppercase)...
Array
(
    [NAME] => Amit
    [CITY] => Gwalior
)





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.