Home >Backend Development >PHP Problem >How to convert php array key values to lowercase
In php, you can use the array_change_key_case function to convert the array key value to lowercase. The syntax format is "array_change_key_case(array, CASE_LOWER)". When the value is CASE_LOWER, converts the array's keys to lowercase letters.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
Convert the array keys to lowercase in PHP, We can easily implement this without using loops. We just need to use array_change_key_case(). The array_change_key_case function has two parameters, one is an array, and the other can be the constant "CASE_LOWER", so we may need to do this when doing large projects.
The following will introduce how to use array_change_key_case() to convert array values to lowercase.
The PHP code example is as follows:
<?php $myArray = ['Hey'=>'Hey','HELLO'=>'Hello','hi'=>'Hi','Gm'=>'GM']; $result = array_change_key_case($myArray, CASE_LOWER); print_r($result);
Output:
Array Array ( [hey] => Hey [hello] => Hello [hi] => Hi [gm] => GM )
As shown above, the keys of the associative array are converted to lowercase.
Function introduction:
array_change_key_case() Modifies all key names in the array to all uppercase or lowercase array_change_key_case ( array $array [, int $case = CASE_LOWER ] ) : array
array_change_key_case() Changes all key names in the array array to all lowercase or uppercase. This function does not change the numeric index.
Parameters:
array, the array to be operated on.
case, you can use two constants here, CASE_UPPER or CASE_LOWER (default value).
Return value, return an array whose keys are all lowercase or all uppercase; if the input value (array) is not an array, then return FALSE
Note: If If the input value (array) is not an array, an error warning (E_WARNING) will be thrown.
Recommended: "2021 PHP interview questions summary (collection)" "php video tutorial》
The above is the detailed content of How to convert php array key values to lowercase. For more information, please follow other related articles on the PHP Chinese website!