Home > Backend Development > PHP Problem > How to batch change key names in php arrays

How to batch change key names in php arrays

PHPz
Release: 2023-04-19 10:34:58
Original
623 people have browsed it

In PHP, array is a very practical data structure that can store and process multiple values. However, in actual development, we often need to batch modify the key names of arrays. In this case, we need to use the array-related functions to complete.

Below, I will introduce in detail how to batch modify array key names in PHP. Specifically, we will use the following function:

  1. array_combine(): Use the value of one array as the key of the new array, and the value of the other array as the value of the new array;
  2. array_keys(): Returns all the key names in the array;
  3. array_values(): Returns all the values ​​in the array;
  4. array_map(): Apply the specified function to each array element, And returns a new array;
  5. array_walk(): Apply a user-defined function to each element in the array.

Now, we assume there is an array $arr, which contains several elements, each element has a numeric key name. We want to convert them into string key names, such as replacing "0" with "apple", replacing "1" with "banana", etc. So, what to do? Please look at the following code:

$arr = array('apple', 'banana', 'orange', 'peach', 'lemon');
$key_names = array('apple', 'banana', 'orange', 'peach', 'lemon');
$new_arr = array_combine($key_names, $arr);
print_r($new_arr);
Copy after login

In this code, we first define an $arr array and a $key_names array, whose number of elements is the same. Next, we use the array_combine() function to use the value of the $arr array as the value of the new array and the value of the $key_names array as the key of the new array. Finally, we print out the contents of the new array $new_arr through the print_r() function, and we can see the result we want:

Array
(
    [apple] => apple
    [banana] => banana
    [orange] => orange
    [peach] => peach
    [lemon] => lemon
)
Copy after login

In this way, we successfully converted the key name from a number to a string . But in some cases, our needs may be more complex. For example, we need to convert all key names from uppercase letters to lowercase letters. At this time, we can use the array_keys() and array_map() functions to achieve this. The code is as follows:

$arr = array(
    'Apple' => 'red',
    'Banana' => 'yellow',
    'Orange' => 'orange',
    'Peach' => 'pink',
    'Lemon' => 'yellow'
);

$lowercase_keys = array_map('strtolower', array_keys($arr));
$new_arr = array_combine($lowercase_keys, array_values($arr));

print_r($new_arr);
Copy after login

In this code, we first define an $arr array and capitalize the first letter of its key name. Next, we use the array_keys() function to return all the keys of the $arr array. We then use the array_map() function to apply the strtolower() function to each key name, converting them all to lowercase. Finally, we combine the new key name and the value of the $arr array using the array_combine() function to form a new array $new_arr. Use the print_r() function to print out the contents of the new array $new_arr, and you can see the modified result:

Array
(
    [apple] => red
    [banana] => yellow
    [orange] => orange
    [peach] => pink
    [lemon] => yellow
)
Copy after login

In this way, we have successfully converted all key names into lowercase letters. It should be noted that when using the array_combine() function, make sure that the key names and the number of values ​​of the new array are equal, otherwise an error will occur. Here, we use the array_values() function to get all the values ​​of the $arr array to ensure that their number is the same as the number of $lowercase_keys array.

Of course, the above methods are only for demonstration. In actual use, you need to choose the appropriate functions and methods according to the specific situation.

In addition to the methods introduced above, there is also a more flexible way, which is to use the array_walk() function. This function can customize the callback function to operate on each element in the array, so it can meet more diverse needs.

Below, we implement a simple example to add the prefix "fruit_" to all key names in the array. The code is as follows:

$arr = array(
    'apple' => 'red',
    'banana' => 'yellow',
    'orange' => 'orange',
    'peach' => 'pink',
    'lemon' => 'yellow'
);

function add_prefix(&$value, $key, $prefix) {
    $value = $prefix . '_' . $key;
}

array_walk($arr, 'add_prefix', 'fruit');
print_r($arr);
Copy after login

In this code, we first define a $arr array, and defines an add_prefix() function. The add_prefix() function has three parameters, which are the value to be operated on, the key name of the current value and the specified prefix. Inside the function, we concatenate the key name and prefix of the current value to generate a new key name and assign it to the key name of the current value. Then, we use the array_walk() function to apply the add_prefix() function to each element in the $arr array, and finally print out the modified array:

Array
(
    [fruit_apple] => red
    [fruit_banana] => yellow
    [fruit_orange] => orange
    [fruit_peach] => pink
    [fruit_lemon] => yellow
)
Copy after login

In this way, we have successfully implemented batch modification of array keys. name operation. In general, there are many built-in functions in PHP that can operate on arrays, and you can choose the most appropriate method according to your actual needs.

The above is the detailed content of How to batch change key names in php arrays. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template