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

How to convert array keys to lowercase in PHP?

藏色散人
Release: 2023-04-05 15:00:01
Original
3817 people have browsed it


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);
Copy after login

Output:

Array

Array
(
    [hey] => Hey
    [hello] => Hello
    [hi] => Hi
    [gm] => GM
)
Copy after login

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
Copy after login

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!

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