A detailed explanation of the array_change_key_case() function in PHP

PHP中文网
Release: 2023-03-16 19:26:02
Original
1172 people have browsed it

array_change_key_case() Converts all keys of the array to uppercase letters:

<?php 
/* 
 array_change_key_case() 返回其键均为大写或小写的数组。 
 array array_change_key_case(array input[,int case]) 
 参数描述:array是要转换键值的数组 
     case有两个选项:CASE_LOWER,默认选项,以小写字母返回数组的键 
             CASE_UPPER,以大写字母返回数组的键 
 */
$input_array = array(&#39;a&#39;=>&#39;Java&#39;,  
           &#39;B&#39;=>&#39;Php&#39;,  
           &#39;c&#39;=>&#39;C++&#39;,  
           &#39;D&#39;=>&#39;C#&#39;); 
print_r(array_change_key_case($input_array, CASE_LOWER)); 
print_r(array_change_key_case($input_array, CASE_UPPER)); 
//如果在运行该函数时两个或多个键相同,则最后的元素会覆盖其他元素,例如: 
$input_array = array(&#39;a&#39;=>&#39;Barcelona&#39;,  
           &#39;B&#39;=>&#39;Madrid&#39;,  
           &#39;c&#39;=>&#39;Manchester&#39;,  
           &#39;b&#39;=>&#39;Milan&#39;); 
print_r(array_change_key_case($input_array, CASE_LOWER)); 
?>
Copy after login

Definition and usage

array_change_key_case() function converts all keys of the array to uppercase letters or lowercase letter.

Syntax

array_change_key_case(array,case);
Copy after login

Parameters

Description

array Required. Specifies the array to use.

case Optional. Possible values:

CASE_LOWER - Default value. Convert an array's keys to lowercase letters.

CASE_UPPER - Convert array keys to uppercase letters.

Technical Details

Return value:

Returns an array with keys with lowercase letters, or returns with uppercase letters An array of letters with keys, or FALSE if array is not an array.

PHP version:

4.2+

Convert all keys of the array to lowercase letters:

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
print_r(array_change_key_case($age,CASE_LOWER));
?>
Copy after login

If after running array_change_key_case() there are two or more keys with the same key (such as "b" and "B"), the last element will overwrite the other elements:

<?php
$pets=array("a"=>"Cat","B"=>"Dog","c"=>"Horse","b"=>"Bird");
print_r(array_change_key_case($pets,CASE_UPPER));
?>
Copy after login

More examples :

$cc = [
            &#39;0&#39;=>[
                &#39;Abc&#39;=>&#39;asdfasdf&#39;,
                &#39;BBAbc&#39;=>&#39;asdfasdf&#39;,
                &#39;AbDDc&#39;=>&#39;asdfasdf&#39;,
                
            ]
        ];
        $tmp = array_change_key_case($cc);
        dump($tmp);
        return;
Copy after login

The printout result is

array (size=1)
  0 => 
    array (size=3)
      &#39;Abc&#39; => string &#39;asdfasdf&#39; (length=8)
      &#39;BBAbc&#39; => string &#39;asdfasdf&#39; (length=8)
      &#39;AbDDc&#39; => string &#39;asdfasdf&#39; (length=8)
Copy after login

The above is the detailed content of A detailed explanation of the array_change_key_case() function in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!