Home >Backend Development >PHP Problem >How to convert php array key values ​​to lowercase

How to convert php array key values ​​to lowercase

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-06-01 11:59:111639browse

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.

How to convert php array key values ​​to lowercase

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 = [&#39;Hey&#39;=>&#39;Hey&#39;,&#39;HELLO&#39;=>&#39;Hello&#39;,&#39;hi&#39;=>&#39;Hi&#39;,&#39;Gm&#39;=>&#39;GM&#39;];
$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!

Statement:
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