Home > Backend Development > PHP Problem > How to modify the key name of an array in php

How to modify the key name of an array in php

PHPz
Release: 2023-04-20 15:36:44
Original
659 people have browsed it

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

Output result:

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

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

Output result:

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

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

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

)
Copy after login

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

Output result:

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

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!

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