Home  >  Article  >  Backend Development  >  How to change key value in php array

How to change key value in php array

王林
王林Original
2023-05-19 09:47:37506browse

In PHP, there are many ways to change the key value of an array. The following are some common methods:

1. Use the array_combine() function

array_combine() function to The two arrays are combined into a new array, one of which is the key name, and the other array is the key value. Through this function, the key name and key value of the original array can be exchanged:

$original_array = array('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3');
$new_keys = array('new_key1', 'new_key2', 'new_key3');

$flipped_array = array_combine($new_keys, $original_array);
print_r($flipped_array);

The output result is :

Array
(
    [new_key1] => value1
    [new_key2] => value2
    [new_key3] => value3
)

2. Use array_flip() function

array_flip() function is used to interchange the key names and key values ​​in the array, using the key names of the original array as the key values, and the original The key value of the array is used as the key name:

$original_array = array('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3');

$flipped_array = array_flip($original_array);
print_r($flipped_array);

The output result is:

Array
(
    [value1] => key1
    [value2] => key2
    [value3] => key3
)

If the key value in the original array is not unique, exchanging the key name and key value through this method will result in some Information is lost.

3. Use foreach loop

Use foreach loop to traverse the elements in the original array one by one, and reassign their key names and key values:

$original_array = array('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3');

$new_keys = array('new_key1', 'new_key2', 'new_key3');

$new_array = array();
foreach ($original_array as $key => $value) {
    $new_key = isset($new_keys[$key]) ? $new_keys[$key] : $key;
    $new_array[$new_key] = $value;
}

print_r($new_array);

The output result is:

Array
(
    [new_key1] => value1
    [new_key2] => value2
    [new_key3] => value3
)

This method is more flexible and new key names can be customized as needed.

4. Use array_map() function

array_map() function is used to apply a callback function to each element in the array, and use the return value of the callback function as the element value of the new array. Through this method, the key names in the original array can be modified:

$original_array = array('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3');

$new_array = array_map(function($value) {
    return $value . '_new';
}, $original_array);

print_r($new_array);

The output result is:

Array
(
    [key1] => value1_new
    [key2] => value2_new
    [key3] => value3_new
)

Through this method, the key names in the original array can be easily modified, but The modification method is relatively fixed, and key names can only be spliced ​​and modified directly in the callback function.

Summary

There are many ways to change the key value of an array in PHP. You can choose the most appropriate method according to the actual situation. Commonly used ones include array_combine() function, array_flip() function, foreach loop and array_map() function, etc. If you need to customize a new key name, it is recommended to use the foreach loop; if you want to directly modify the original key name, you can use the array_map() function. The key names and key values ​​of the array are very important for the use of the array. Reasonable modifications as needed can make the program more flexible and efficient.

The above is the detailed content of How to change key value in php array. 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