Home > Article > Backend Development > How to convert key names in array in php
PHP is a scripting language widely used in the field of web development. In PHP, arrays are a common data type that are often used to store and process data. In some data processing, it is often necessary to convert the key names of arrays. This article will introduce how to use PHP to convert the key names of arrays into other forms.
1. Use the array_map() function
The array_map() function is a PHP built-in array function, which can apply a callback function to each element in one or more arrays. We can use this function to iterate through the array and convert each key name.
Sample code:
$arr = array('name1' => 'value1', 'name2' => 'value2', 'name3' => 'value3'); $new_arr = array_map(function ($key) { return strtolower(str_replace('name', 'id', $key)); }, array_keys($arr)); print_r(array_combine($new_arr, $arr));
In this code snippet, we first define an array $arr and use the array_map() function to traverse its key names. Use the array_keys() function to return all keys of the array. In the callback function, we use the strtolower() function to convert the key name to lowercase, and use the str_replace() function to convert "name" to "id".
Finally, we use the array_combine() function to recombine the new key name and the original value into a new array.
2. Use foreach loop
In addition to the array_map() function, we can also use the foreach loop to traverse the array and convert each key name.
Sample code:
$arr = array('name1' => 'value1', 'name2' => 'value2', 'name3' => 'value3'); $new_arr = array(); foreach ($arr as $key => $value) { $new_key = strtolower(str_replace('name', 'id', $key)); $new_arr[$new_key] = $value; } print_r($new_arr);
In this code snippet, we use a foreach loop to iterate through the array. In the loop, we use the strtolower() function to convert the key name to lowercase, and the str_replace() function to convert "name" to "id".
Finally, we combine the new key names and the original values into a new array.
3. Use the array_combine() function
Another way to convert the key names of the array into other forms is to use the array_combine() function.
Sample code:
$arr = array('name1' => 'value1', 'name2' => 'value2', 'name3' => 'value3'); $new_arr = array(); $new_keys = array_map(function ($key) { return strtolower(str_replace('name', 'id', $key)); }, array_keys($arr)); $new_arr = array_combine($new_keys, $arr); print_r($new_arr);
In this code snippet, we use the array_map() function to iterate over the key names of the array and convert them into new key names. Then, we use the array_combine() function to recombine the new keys and original values into a new array.
Summary
In PHP, we can use the array_map() function, foreach loop or array_combine() function to convert the key names of the array into other forms. These methods are suitable for different needs and scenarios. In practical applications, we can choose the most appropriate method for conversion according to the specific situation.
The above is the detailed content of How to convert key names in array in php. For more information, please follow other related articles on the PHP Chinese website!