Home >Backend Development >PHP Problem >How to convert key value in php array
PHP is an object-oriented programming language that provides a powerful array processing capability. In actual development, we often encounter situations where we need to adjust arrays. One of the common requirements is to convert the keys of the array. This article will introduce how to convert keys in PHP arrays, and explain in detail to readers the issues that need to be paid attention to in the process.
1. Use the array_combine() function
The array_combine() function is a function in PHP used to merge two arrays and generate a new array. The usage is as follows:
array_combine(array $keys, array $values): array
The first parameter $keys of this function is an array, which represents the key name of the new array; The second parameter $values is also an array, which represents the key value of the new array. Their number of elements must be equal, otherwise false will be returned.
The order of elements in the $keys and $values arrays must correspond one to one. For example, the first element of the $keys array corresponds to the first element of the $values array.
The following is an example that converts an array $name with a number as a subscript into an array $new_name with a string as a subscript:
$name = array('0' => 'Alice', '1' => 'Bob', '2' => 'Carol'); $new_name = array_combine(array('a', 'b', 'c'), $name); print_r($new_name);
The output result is:
Array ( [a] => Alice [b] => Bob [c] => Carol )
In this example, the numeric subscripts in the $name array are converted into string subscripts, while the values in the array remain unchanged. This is because we used the array_combine() function and gave it new key names.
2. Use the array_flip() function
The array_flip() function is a function in PHP used to exchange keys and values in an array. The usage is as follows:
array_flip(array $trans) : array
The parameter $trans of this function is an array, which represents the exchanged array. It will return a new array where the keys in the original array become the values in the new array and the values in the original array become the keys in the new array.
The following is an example that converts an array $age with a number as a subscript into an array $info with an age value as a subscript:
$age = array('Alice' => 25, 'Bob' => 35, 'Carol' => 30); $info = array_flip($age); print_r($info);
The output result is:
Array ( [25] => Alice [35] => Bob [30] => Carol )
In this example, the string subscripts in the $age array are converted into numeric subscripts, while the values in the array remain unchanged. This is because we used the array_flip() function and passed it the $age array.
It should be noted that if there are duplicate values in the original array, the converted keys will overwrite the previous keys. Therefore, when using the array_flip() function, you need to ensure that the values in the original array are unique.
3. Use the array_map() function
The array_map() function is a function in PHP used to apply a callback function to each element of an array. The usage is as follows:
array_map(callable $callback, array ...$arrays) : array
The first parameter of this function $callback is a callback function, which will be applied to the array on each element in . The array after the second parameter is the array that needs to be operated. Their number of elements may not be equal. This function will generate a new array, each element of which is the return value of the $callback function.
The following is an example that converts an array $price with numbers as subscripts into an array $actual_price with floating point numbers as subscripts:
$price = array('Alice' => 10, 'Bob' => 15.5, 'Carol' => 20); $actual_price = array_map(function($p) { return round($p * 0.8, 2); }, $price); print_r($actual_price);
The output result is:
Array ( [Alice] => 8.00 [Bob] => 12.40 [Carol] => 16.00 )
In this example, we use the array_map() function and pass it a callback function. This function multiplies each price value by 0.8 and rounds to two decimal places, then returns a new price value. Finally, we get a new array $actual_price, in which the subscript becomes the person's name, and the value in the array is the calculated actual price.
It should be noted that the $callback function must return a value, otherwise a warning will be generated.
4. Use foreach loop
In addition to the above functions, you can also manually convert the keys of the array by using a foreach loop. The following is an example that converts an array $id with a numeric subscript into an array $name_id with a string subscript:
$id = array(1001, 1002, 1003); $name = array('Alice', 'Bob', 'Carol'); $name_id = array(); foreach($id as $i => $val) { $name_id[$name[$i]] = $val; } print_r($name_id);
The output result is:
Array ( [Alice] => 1001 [Bob] => 1002 [Carol] => 1003 )
In this example , we used a foreach loop and converted the array in the loop. Specifically, we iterate through each element in the $id array and use the subscript of the element to retrieve the corresponding value from the $name array. We then add this new key-value pair to the $name_id array.
It should be noted that when using a foreach loop, if you want to modify the elements in the array, you need to use a reference to the array.
5. Summary
This article introduces how to convert keys in PHP arrays, including methods of using array_combine() function, array_flip() function, array_map() function and foreach loop. Each of these methods has different characteristics, and different methods can be selected according to actual needs.
When using these methods, it is important to note that the new key name must be unique, and different methods may produce different results. Therefore, you need to think carefully and choose an appropriate method when converting the keys of an array.
The above is the detailed content of How to convert key value in php array. For more information, please follow other related articles on the PHP Chinese website!