Home  >  Article  >  Backend Development  >  How to convert array key to uppercase in php

How to convert array key to uppercase in php

青灯夜游
青灯夜游Original
2021-07-15 15:40:113416browse

In PHP, you can use the array_change_key_case() function to convert the array key to uppercase. This function can convert all the key names of the array to uppercase letters or lowercase letters. The syntax "array_change_key_case(array,CASE_UPPER );".

How to convert array key to uppercase in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

In PHP, you can use array_change_key_case () function to convert the array key to uppercase.

Syntax format:

array_change_key_case(array,case);

array_change_key_case() function will change all key names in the array array to all lowercase or uppercase according to case. The default is to lowercase. This function does not change the numeric index.

Parameters Description
array Required. Specifies the array to use.
case Optional. 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 whose keys are all lowercase or all uppercase; if the input value (array) is not an array, then return false

Description: If the input value (array) is not an array, an error warning (E_WARNING) will be thrown.

Example: Convert array key to uppercase

<?php
$input_array = array("FirSt" => 1, "SecOnd" => 4);
print_r(array_change_key_case($input_array, CASE_UPPER));
?>

Output:

Array
(
    [FIRST] => 1
    [SECOND] => 4
)

Recommended learning: "PHP Video Tutorial

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