Home  >  Article  >  Backend Development  >  How to convert array keys to lowercase in PHP?

How to convert array keys to lowercase in PHP?

藏色散人
藏色散人Original
2019-03-11 13:40:453799browse


Convert array keys to lowercase in PHP, we can do it simply without using a loop. 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.

How to convert array keys to lowercase in PHP?

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() changes 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 the array Change all key names in 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 the input value (array) is not An array will throw an error warning (E_WARNING).

Related recommendations: "How to convert array values ​​to lowercase in PHP?

This article is an introduction to the method of converting array keys to lowercase in PHP. It is simple and easy to understand. I hope it will be helpful to friends in need!


The above is the detailed content of How to convert array keys to lowercase in PHP?. 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