Home  >  Article  >  Backend Development  >  How to modify associative array key in php

How to modify associative array key in php

PHPz
PHPzOriginal
2023-04-18 10:20:38464browse

Associative array in PHP is a very commonly used data type, which can be used to store key-value pairs. When we need to modify the key of an associative array, we usually face some problems. This article will introduce how to modify the key of an associative array in PHP.

1. Use the array_combine() function

PHP’s array_combine() function can combine two arrays into an associative array. We can use this function to modify the key of an associative array. The specific steps are as follows:

  1. Use the array_keys() function to get all the keys of the associative array and save them in a new array;
  2. Use the array_values() function to get all the values ​​of the associative array , saved to another new array;
  3. Use the array_combine() function to combine the new key array and value array into a new associative array.

The code is as follows:

// 原始数组
$original_array = array(
    'name' => 'Tom',
    'age' => 25,
    'gender' => 'male'
);

// 将原始数组中的key修改为新的值
$new_keys = array('name', 'years_old', 'gender');
$original_values = array_values($original_array);
$new_array = array_combine($new_keys, $original_values);

Through the above code, we can modify the key 'age' in the original array to 'years_old'.

2. Loop to modify the array

We can also use the loop method to modify the keys in the array one by one. The specific steps are as follows:

  1. Use a foreach loop to traverse the associative array;
  2. In the loop body, use the unset() function to delete the original key and re-add the item using the new key.

The code is as follows:

// 原始数组
$original_array = array(
    'name' => 'Tom',
    'age' => 25,
    'gender' => 'male'
);

// 将原始数组中的key修改为新的值
$new_keys = array('name', 'years_old', 'gender');
foreach ($original_array as $key => $value) {
    unset($original_array[$key]);
    $modified_key = $new_keys[array_search($key, array_keys($original_array))];
    $original_array[$modified_key] = $value;
}

Through the above code, we can modify the key 'age' in the original array to 'years_old'.

3. Use the array_map() function

We can also use the array_map() function to operate all keys in the form of functions. The specific steps are as follows:

  1. Create an anonymous function and modify the original key to a new key inside the function;
  2. Use the array_map() function to call the above function to modify all keys.

The code is as follows:

// 原始数组
$original_array = array(
    'name' => 'Tom',
    'age' => 25,
    'gender' => 'male'
);

// 将原始数组中的key修改为新的值
$new_keys = array('name', 'years_old', 'gender');
$new_array = array_map(function ($key) use ($new_keys) {
    $modified_key = $new_keys[array_search($key, array_keys($original_array))];
    return $modified_key;
}, array_keys($original_array));

$new_array = array_combine($new_array, array_values($original_array));

Through the above code, we can modify the key 'age' in the original array to 'years_old'.

Summary:

The above three methods can be used to modify associative arrays in PHP. The specific method selection depends on different application scenarios. If there are fewer keys that need to be modified, we can choose to use the loop method; if there are more keys that need to be modified, we can use the array_combine() function; if more complex processing operations are required for each key, you can use array_map( )function. No matter which method is used, we need to pay attention to some basic PHP knowledge, such as array index, Key value, etc.

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