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

How to change the key value of an array in php

PHPz
PHPzOriginal
2023-04-27 09:11:22859browse

In PHP development, it is often necessary to operate on arrays. The key value (also called index) in the array is very important. Sometimes, we need to change some key values ​​in the array to meet business needs. Next, I will introduce some methods to change the key value of PHP array.

1. Use array_combine() function

array_combine() function can combine two arrays into one array. The key value of the first array will be used as the index of the new array, and the key value of the second array will be used as the index of the new array. The values ​​of the array will be used as elements of the new array. We can use this to change the key values ​​of the array.

Suppose we have the following array:

$old_array = array('a'=>'apple', 'b'=>'banana', 'c'=>'cherry');

Now we want to change the key value of a to aa, we can use the following code:

$new_key = array('a' => 'aa');
$new_array = array_combine(array_replace(array_keys($old_array), $new_key), $old_array);

Here we define a new array $new_key, pass in the key value a to be changed and the new key value aa. Next, we use the array_replace() function to extract the key value a to be changed, and then pass it into the array_combine() function, and finally return a new array $new_array.

2. Use the array_map() function

The array_map() function can pass each element in the array to the specified callback function for processing and return a new array. We can define a simple callback function to change the array key value.

Suppose we have the following array:

$old_array = array('a'=>'apple', 'b'=>'banana', 'c'=>'cherry');

Now we want to change the key value of b to bb, we can use the following code:

$new_array = array_map(function($key, $value) use ($old_array){
    if($key == 'b'){
        return array('bb' => $value);
    } else {
        return array($key => $value);
    }
}, array_keys($old_array), $old_array);
$new_array = array_merge(...$new_array);

Here, we use an anonymous Function serves as a callback function for the array_map() function. In this anonymous function, we first determine whether the key value of the current element is equal to 'b'. If equal, change its key value to 'bb' and return the current element as an array.

Finally, we use the array_merge() function to merge the returned arrays into a new array.

3. Use the array_walk() function

The array_walk() function can operate on each element in the array and return a new array. By overriding this function, we can change the key value of array elements.

Similarly, we assume that we have the following array:

$old_array = array('a'=>'apple', 'b'=>'banana', 'c'=>'cherry');

Now we want to change the key value of c to cc, we can use the following code:

$new_array = array();
array_walk($old_array, function($value, $key) use ($old_array, &$new_array){
    if($key == 'c'){
        $new_array['cc'] = $value;
    } else {
        $new_array[$key] = $value;
    }
});

In this example, We define an empty array $new_array to save the changed array. Through the array_walk() function, we traverse each element in the original array $old_array.

In the callback function, we first determine whether the key value of the current element is equal to 'c'. If it is equal, change its key value to 'cc' and add it to the new array $new_array. Otherwise, the current element is added directly to the new array $new_array.

Finally, we get a new array $new_array, whose key value c has been changed to cc.

Summary

The above are three ways to change the key value of a PHP array. In actual development, we can choose the method that best suits us according to our needs. Whether you use the array_combine(), array_map() or array_walk() function, you can easily change the array key value.

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