PHP array_change_key_case()函数与示例

PHP array_change_key_case()函数 (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.

array_change_key_case()函数是基于数组的函数,用于更改数组中所有键的大小写(小写或大写)。

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.

    array_name是名称,是我们必须更改键大小写的数组的名称。

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

    case_value是一个可选参数,它有两个值。 CASE_LOWER –将所有键转换为小写字母; CASE_UPPER –将所有键转换为大写字母。 其默认值为“ CASE_LOWER”。 如果不使用此参数,则数组的所有键都将转换为小写。

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代码:

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


翻译自: https://www.includehelp.com/php/array_change_key_case-function-with-example.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值