Array Functions
Advertisements

PHP array_change_key_case() Function

Topic: PHP Array ReferencePrev|Next

Description

The array_change_key_case() function is used to change the case of all keys in an array to lowercase or uppercase. Numbered indices are left as is.

The following table summarizes the technical details of this function.

Return Value: Returns an array with its keys lower or uppercased, or FALSE if array is not an array.
Version: PHP 4.2+

Syntax

The basic syntax of the array_change_key_case() function is given with:

array_change_key_case(array, case)

The following example shows the array_change_key_case() function in action.

<?php
// Sample array
$persons = array("Harry"=>22, "Clark"=>32, "John"=>28);

// Changing keys to uppercase
print_r(array_change_key_case($persons, CASE_UPPER));
?>

Parameters

The array_change_key_case() function accepts the following parameters.

Parameter Description
array Required. Specifies the array to work on.
case

Optional. Specifies the case. Possible values are:

CASE_LOWER – Changes the keys to lowercase. This is default.

CASE_UPPER – Changes the keys to uppercase.

Note: If you do not specify the case parameter inside the array_change_key_case() function, all keys are converted to lowercase letter, because CASE_LOWER is the default case value.


More Examples

Here're some more examples showing how array_change_key_case() function basically works:

In the following example the array keys are converted to lowercase letters:

<?php
// Sample array
$persons = array("Harry"=>22, "Clark"=>32, "John"=>28);

// Changing keys to lowercase
print_r(array_change_key_case($persons));
?>

If two or more keys will be same after running array_change_key_case() (e.g. "keY" and "kEY"), the value that is later in the array will override the previous ones.

<?php
// Sample array
$persons = array("Harry"=>22, "Clark"=>32, "harry"=>28);

// Changing keys to lowercase
print_r(array_change_key_case($persons));
?>
Advertisements
Bootstrap UI Design Templates

Your Feedback:

We would love to hear from you, please drop us a line.