PHP Tutorial

PHP Examples

PHP Practice

PHP Array array_change_key_case() Function (With Examples)

In this tutorial, we will learn about the PHP array_change_key_case() function with its usage, syntax, parameters, return value, and examples. By IncludeHelp Last updated : December 31, 2023

PHP array_change_key_case() Function

The 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

The syntax of the array_change_key_case() function:

array_change_key_case(array_name, [case_value]) : array

Parameters

The parameters of the array_change_key_case() function:

  • 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.

Return Value

The return type of this method is array, it returns an array with the keys which are converted either is lowercase or uppercase.

Sample Input/Output

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 array_change_key_case() Function Example

<?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

The output of the above example is:

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
)

To understand the above example, you should have the basic knowledge of the following PHP topics:

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.