Home Backend Development PHP Problem How to convert array key names to uppercase using PHP

How to convert array key names to uppercase using PHP

Apr 18, 2023 am 10:20 AM

As a popular programming language, PHP has powerful array processing functions. When processing arrays, sometimes you need to perform some operations on the array key names, such as converting the array key names to uppercase. This article will introduce how to use PHP to convert array key names to uppercase.

Method 1: Use array_change_key_case() function

PHP provides an array_change_key_case() function for converting array key names to uppercase and lowercase. This function has two parameters. The first parameter is the array that requires key name operation, and the second parameter specifies the case type of conversion (the optional value is CASE_LOWER or CASE_UPPER).

The following is an example of converting the array key name to uppercase:

$myArray = array("name" => "John", "age" => 30);
$myArray = array_change_key_case($myArray, CASE_UPPER);
print_r($myArray);
Copy after login

Execute the above code, the output result is as follows:

Array
(
    [NAME] => John
    [AGE] => 30
)
Copy after login
Copy after login
Copy after login

Method 2: Use array_combine() function

Another method is to use the array_combine() function. This function merges two arrays into a new array, where the key name of one array will be used as the key name of the new array, and the value of the other array will be used as the value of the new array. Therefore, you can use an array containing uppercase key names as the key array, and then use the original array as the value array to generate a new array.

The following is an example of using the array_combine() function to convert the array key name to uppercase:

$myArray = array("name" => "John", "age" => 30);
$uppercaseKeys = array_map('strtoupper', array_keys($myArray));
$newArray = array_combine($uppercaseKeys, $myArray);
print_r($newArray);
Copy after login

Execute the above code, the output result is as follows:

Array
(
    [NAME] => John
    [AGE] => 30
)
Copy after login
Copy after login
Copy after login

Method 3: Use array_map() function

Another method is to use the array_map() function to operate on the key names of the array. This function applies a callback function to each element of the array, where the strtoupper() function is used to convert the key name to uppercase. Finally a new array is generated.

The following is an example of using the array_map() function to convert array key names to uppercase:

$myArray = array("name" => "John", "age" => 30);
$newArray = array_combine(array_map('strtoupper', array_keys($myArray)), $myArray);
print_r($newArray);
Copy after login

Execute the above code, the output results are as follows:

Array
(
    [NAME] => John
    [AGE] => 30
)
Copy after login
Copy after login
Copy after login

Summary

This article introduces three methods of converting PHP array key names to uppercase: using the array_change_key_case() function, using the array_combine() function, and using the array_map() function. These techniques are easy to use and can improve the efficiency of PHP array processing. If you have similar needs, you can choose a method that suits you according to your needs.

The above is the detailed content of How to convert array key names to uppercase using PHP. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24