Home > Backend Development > PHP Problem > How to convert array key name (key) to lowercase in php

How to convert array key name (key) to lowercase in php

青灯夜游
Release: 2023-03-16 12:30:02
Original
1817 people have browsed it

In PHP, you can use the array_change_key_case() function to convert the array key name (key) to lowercase; this function can convert all the keys (keys) in the array to lowercase letters, you only need to omit the first Two parameters or set the second parameter to "CASE_LOWER", the syntax is "array_change_key_case (array)" or "array_change_key_case (array, CASE_LOWER)".

How to convert array key name (key) to lowercase in php

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

How to convert the array key name (key) in php for lowercase? Simple!

PHP provides a function: array_change_key_case() to achieve this function. Let's take a look at this function

array_change_key_case() The function can convert all keys of the array into uppercase letters or lowercase letters. Syntax format:

array_change_key_case(array,case);
Copy after login
##ParametersDescriptionarrayRequired. Specifies the array to use. caseOptional. Possible values:
    CASE_LOWER - Default value. Convert an array's keys to lowercase letters.
  • CASE_UPPER - Convert array keys to uppercase letters.
Return value: Returns an array with keys with lowercase letters, or returns an array with keys with uppercase letters, or if array is not a Array returns FALSE.


alright! After understanding the array_change_key_case() function, we also know how to convert the array key name (key) to lowercase:

  • Just need to set the second parameter of the array_change_key_case() function to " CASE_LOWER", or omit the second parameter.

Implementation code:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);  
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
echo "原数组:";
var_dump($age);
echo "数组键名(key)转为小写:";
var_dump(array_change_key_case($age));
var_dump(array_change_key_case($age,CASE_LOWER));
?>
Copy after login

How to convert array key name (key) to lowercase in php

Recommended learning: "

PHP video tutorial

The above is the detailed content of How to convert array key name (key) 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