Hướng dẫn
Quảng cáo

Hàm array_change_key_case() trong PHP

Hướng dẫn cách sử dụng hàm array_change_key_case() về mảng trong lập trình PHP

Tác dụng của hàm array_change_key_case()

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.

Example

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:

Example

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.

Example

22, "Clark"=>32, "harry"=>28);

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

 

 

Bài viết này đã giúp ích cho bạn?

Bài viết mới

Advertisements