Home > Article > Backend Development > How to merge two different arrays in php
In PHP programming, arrays are a commonly used data type that often need to be merged and reorganized. Two different methods of merging and reorganizing arrays are discussed below.
First: Use the array_merge() function to merge arrays
PHP provides an array_merge() function, which allows two or more groups to be merged into one array. The array_merge() function can accept any number of parameters, and each parameter can be an array.
The code is as follows:
$array1 = array('a', 'b', 'c'); $array2 = array('d', 'e', 'f'); $result = array_merge($array1, $array2); print_r($result); // 输出:Array ( [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f )
In the above code, $array1 and $array2 are two different arrays. Use the array_merge() function to merge them into a new array $result. The combined array can be output through the print_r() function.
Second: Use foreach() loop to merge arrays
In addition to the array_merge() function, you can also use foreach() loop to merge two arrays. This method requires iterating through two arrays and adding their elements to a new array one by one.
The code is as follows:
$array1 = array('a', 'b', 'c'); $array2 = array('d', 'e', 'f'); $result = array(); foreach ($array1 as $value) { $result[] = $value; } foreach ($array2 as $value) { $result[] = $value; } print_r($result); // 输出:Array ( [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f )
In the above code, an empty array $result is first created, and then the $array1 and $array2 arrays are traversed respectively, and their elements are added to the $result array one by one. middle. Finally, use the print_r() function to output the merged array.
Reorganize arrays
In addition to merging arrays, you can also reorganize arrays. Reorganizing an array can change the order of elements in the array or reassign keys to elements in the array.
The following discusses two commonly used methods of reorganizing arrays: shuffle() function and array_combine() function.
First method: Use the shuffle() function to rearrange the array
PHP provides a shuffle() function, which can randomly arrange the elements in the array. The shuffle() function does not need to add parameters, it will directly modify the original array.
The code is as follows:
$array = array('a', 'b', 'c', 'd', 'e', 'f'); shuffle($array); print_r($array); // 输出:Array ( [0] => e [1] => d [2] => c [3] => a [4] => f [5] => b )
In the above code, $array is an array, and its elements are randomly arranged using the shuffle() function. You can use the print_r() function to output the rearranged array.
Second: Use the array_combine() function to reorganize the array
In addition to rearranging the array, you can also use the array_combine() function to reorganize the key values of the array. The array_combine() function requires two arrays as parameters, one array is used to specify the new key name, and the other array is used to specify the new key value. The number of elements in both arrays must be the same.
The code is as follows:
$keys = array('a', 'b', 'c'); $values = array(1, 2, 3); $result = array_combine($keys, $values); print_r($result); // 输出:Array ( [a] => 1 [b] => 2 [c] => 3 )
In the above code, $keys and $values are two arrays. Use the array_combine() function to merge them into a new array $result. Among them, the $keys array specifies the key name of the new array, and the $values array specifies the key value of the new array. You can use the print_r() function to output the reorganized array.
Summary
This article introduces two different array merging and reorganization methods. Use the array_merge() function to easily merge multiple arrays, and use the foreach() loop to add array elements one by one. Use the shuffle() function to randomly rearrange array elements, and use the array_combine() function to reorganize the array key values. In actual programming, choosing different methods for processing according to needs can improve the efficiency and readability of the program.
The above is the detailed content of How to merge two different arrays in php. For more information, please follow other related articles on the PHP Chinese website!