Array is one of the very important data structures in the PHP programming language, mainly used to store and process data. In actual development, it is sometimes necessary to modify the key values of the array to meet special needs. This article will introduce how to modify the key value of an array through PHP's built-in functions.
1. array_flip function
The array_flip function can be used to exchange the keys and values of the array. It takes all the keys in the original array as values, all the values in the original array as keys, and returns a new swapped array. The array_flip function can be used to easily modify the key values of the array.
Sample code:
// 原始数组 $array1 = array('name'=>'Tom', 'age'=>20, 'gender'=>'male'); // 通过array_flip函数交换键值 $array2 = array_flip($array1); print_r($array2);
Output result:
Array ( [Tom] => name [20] => age [male] => gender )
You can see that in the array exchanged through the array_flip function, the keys and values in the original array are exchanged.
2. array_walk function
The array_walk function can traverse the array and execute the specified function on each element. We can customize a callback function and rename the key of each element in the original array through the array_walk function. In this way, the key values of the array can be modified through the array_walk function.
Sample code:
// 原始数组 $array3 = array('name'=>'Tom', 'age'=>20, 'gender'=>'male'); // 自定义回调函数 function modify_key(&$value, $key) { // 将原来的键名修改为新的键名 if($key == 'name') $key = 'username'; if($key == 'age') $key = 'userage'; if($key == 'gender') $key = 'usergender'; // 修改后的键名赋值给原始数组 $value = array($key=>$value); } // 使用array_walk函数遍历数组并修改键名 array_walk($array3, 'modify_key'); print_r($array3);
Output result:
Array ( [0] => Array ( [username] => Tom ) [1] => Array ( [userage] => 20 ) [2] => Array ( [usergender] => male ) )
As you can see, through the custom callback function and array_walk function, we successfully made the key values of the original array Modified.
3. array_map function
The array_map function can execute the specified callback function for each element in the array and return a new array. In the callback function, we can change the key value of the array by modifying the key name of the element.
Sample code:
// 原始数组 $array4 = array('name'=>'Tom', 'age'=>20, 'gender'=>'male'); // 自定义回调函数 function modify_key_map($value, $key) { // 将原来的键名修改为新的键名 if($key == 'name') $key = 'username'; if($key == 'age') $key = 'userage'; if($key == 'gender') $key = 'usergender'; // 返回修改后的键名和值 return array($key=>$value); } // 使用array_map函数遍历数组并修改键名 $array5 = array_map('modify_key_map', $array4, array_keys($array4)); print_r($array5);
Output result:
Array ( [0] => Array ( [username] => Tom ) [1] => Array ( [userage] => 20 ) [2] => Array ( [usergender] => male ) )
It can be seen that we have successfully modified the key value of the array through the custom callback function and array_map function.
To sum up, we can modify the key value of the array very conveniently through PHP's built-in functions. The array_flip function can directly exchange the keys and values of the original array, while the array_walk function and array_map function can modify the key values of elements through custom callback functions. In actual development, we can choose different methods to modify the key values of the array according to specific needs.
The above is the detailed content of How to change the key of php array. For more information, please follow other related articles on the PHP Chinese website!