PHP array_change_key_case() Function is used to change case of all of the keys in a given array either to lower case or upper case. Numbered indices are left as is.
PHP array_change_key_case() function is an inbuilt function of PHP.
array_change_key_case β Changes the case of all keys in an array
Syntax
array_change_key_case(array, case)
Parameter Values
Parameter | Description |
---|---|
array | Required. Specifies the array to use |
case | Optional. Possible values: CASE_LOWER β Default value. Changes the keys to lowercase CASE_UPPER β Changes the keys to uppercase |
If the convert_case parameter is not passed then itβs default value is taken which is CASE_LOWER.
Return Value
Returns an array with its keys in lowercase or uppercase, or FALSE if array is not an array
Examples
<?php
// PHP code to illustrate array_change_key_case()
// Both the parameters are passed
function change_case($in_array){
return(array_change_key_case($in_array, CASE_UPPER));
}
// Driver Code
$array = array("Anil" => 90, "RagHav" => 80,
"Abdul" => 95, "Peter" => 85, "RISHAV" => 70);
print_r(change_case($array));
?>
<?php
$vow=array("a"=>"a", "e"=>"e", "i"=>"i", "o"=>"o", "u"=>"u");
print_r(array_change_key_case($vow,CASE_UPPER)); //Array( [A]=>a [E]=>e [I]=>i [O]=>o [U]=>u )
?>
<?php
$sal=array("Rahul"=> "10000" , "Ajay"=> "15000" , "Sid"=> "20000" );
print_r(array_change_key_case( $sal, CASE_LOWER ) ); //Array ( [rahul]=> 10000 [ajay]=> 15000 [sid]=> 20000 )
?>
If two or more array keys will be the same after running array_change_key_case( ) function, the latest array will override the other.
<?php
$city=array("a"=> "Agra" , "B"=> "Bhopal" , "c"=> "Chennai" ,"b"=> "Bengaluru" );
print_r(array_change_key_case( $city, CASE_UPPER ) ); //Array ( [A] => Agra [B] => Bengaluru [C] => Chennai )
?>
If we ignore the second parameter in the function array_change_key_case( ) then the keys will be converted to lowercase.
<?php
$age=array("RAHUL"=> "10" , "VIKAS"=> "20" , "SID"=> "25" );
print_r(array_change_key_case( $age) ); //Array ([rahul]=> 10 [vikas]=> 20 [sid]=> 25 )
?>