Home > Article > Backend Development > php modify the key name of the array
In PHP development, it is often necessary to operate on arrays, and modifying the key name of the array is one of the common operations. This article will introduce how to use PHP language to modify the key name of an array.
1. What is an array key?
First of all, we need to clarify what an array key is. In PHP, an array key refers to a unique identifier for each element in the array. Arrays in PHP can use integers and strings as keys. Of course, after PHP 5.4, you can also use constants as key names.
For example, we first define a simple array:
$arr = array('name' => '张三', 'age' => 20, 'gender' => '男');
In the above code, 'name', 'age' and 'gender' are the key names of the elements in the array $arr respectively. .
2. Use the PHP function array_combine() to modify the array key name
Now, we need to change the 'age' key name in the array $arr to 'years'. In PHP, you can use the function array_combine() to create a new array, in which the keys of the new array are repeatedly mapped from the values of the original array.
Specifically, we can first use the array_combine() function to exchange the key names and values in the original array, and then use this function again to perform repeated mapping, where the 'age' index is changed to 'years' :
$arr = array('name' => '张三', 'age' => 20, 'gender' => '男'); // 将原数组中的键名和值互换 $arr_flip = array_flip($arr); // 重复映射生成新数组 $new_keys = array_combine(array('name', 'years', 'gender'), $arr_flip); // 将新数组中的键名和值再次互换 $new_arr = array_flip($new_keys); print_r($new_arr);
In the above code, we first used the PHP function array_flip() to exchange the key name and value of the original array, turning the key name into the value of the value, and turning the value into The keyname of this keyname. Then, we map the new key names and repetitions to the values of the original array, generating a new array of $new_keys. Finally, we use the array_flip() function again to exchange the key names and values of the new array again, and finally get the new array $new_arr.
3. Use the PHP function array_reduce() to modify the key name of the array
In addition to using the array_combine() function, you can also use the PHP function array_reduce() to modify the key name of the array. The function of the array_reduce() function is to reduce all elements in the array through user-defined functions.
The specific operations are as follows:
$arr = array('name' => '张三', 'age' => 20, 'gender' => '男'); $mapping = array('age' => 'years'); $func = function ($carry, $item) use ($mapping) { $key = $item[0]; $value = $item[1]; if (isset($mapping[$key])) { $key = $mapping[$key]; } $carry[$key] = $value; return $carry; }; $new_arr = array_reduce(array_map(null, array_keys($arr), $arr), $func, array()); print_r($new_arr);
In the above code, we first package the original array, that is, the operation done by array_map(null, array_keys($arr), $arr) . Then, we use a custom function $func in the array_reduce() function, which checks each element of the original array, modifies the key name if a mapping exists, and creates a new key in the reorganized array name and value. Finally, we get the new array $new_arr.
Conclusion
This article introduces how to use PHP language to modify the key name of an array. In actual development, correct array operations can improve the operating efficiency and maintainability of the program. This is one of the reasons why we continue to learn and master new technologies.
The above is the detailed content of php modify the key name of the array. For more information, please follow other related articles on the PHP Chinese website!