search
HomeBackend DevelopmentPHP ProblemHow to modify the key name of an array in php

In PHP, array is a very important data type. It can store multiple values ​​and store and access them in the form of key-value pairs. Sometimes we may need to change the name of the array key. In this case, we can use some functions provided by PHP to operate. This article will introduce how to use PHP to change the key name of an array.

1. Use the array_flip() function

The array_flip() function can exchange the key and value of each element in the array, thereby changing the key name of the array.

Sample code:

<?php $old_arr = array(&#39;name&#39;=>'Tom', 'age'=>18, 'gender'=>'male');
$new_arr = array_flip($old_arr);
print_r($new_arr);
?>

Output result:

Array
(
    [Tom] => name
    [18] => age
    [male] => gender
)

It can be seen that the key name in the original array becomes the value, and the value in the original array becomes The key name of the new array.

2. Use the array_map() function

The array_map() function can apply a callback function to each element in the array and return a new array. We can pass a callback function to modify the key name of the array.

Sample code:

<?php $old_arr = array(&#39;name&#39;=>'Tom', 'age'=>18, 'gender'=>'male');
$new_arr = array_map(function($key, $value){
    if ($key == 'name') {
        $key = 'username';
    }
    return array($key=>$value);
}, array_keys($old_arr), $old_arr);
print_r($new_arr);
?>

Output result:

Array
(
    [0] => Array
        (
            [username] => Tom
        )

    [1] => Array
        (
            [age] => 18
        )

    [2] => Array
        (
            [gender] => male
        )

)

As you can see, the key names in the new array have been modified.

3. Use foreach loop

The foreach loop can traverse each element in the array and modify it. We only need to process each element in the loop to modify the key name.

Sample code:

<?php $old_arr = array(&#39;name&#39;=>'Tom', 'age'=>18, 'gender'=>'male');
$new_arr = array();
foreach ($old_arr as $key => $value) {
    if ($key == 'name') {
        $key = 'username';
    }
    $new_arr[$key] = $value;
}
print_r($new_arr);
?>

Output result:

Array
(
    [username] => Tom
    [age] => 18
    [gender] => male
)

As you can see, the key names in the new array have been modified.

To sum up, in PHP, you can change the key name of the array through array_flip(), array_map() and foreach loop. Different methods should be chosen based on actual needs.

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

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 Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.