This article describes how to merge, append, and connect arrays in php. The main functions are array_merge () and array_combine. For more information, see. 1. first, merge the array. The array_merge () function combines arrays and returns a Union array. The obtained array starts with the first input array parameter and is immediately added according to the order in which the following array parameters appear. The format is: array array_merge (array array1 array2 ..., ArrayN) can be used to combine the units of one or more arrays. the values in an array are appended to the values of the previous array. Returns an array of results. If the input array contains the same string key name, the value after the key name overwrites the previous value. However, if the array contains a number key name, the subsequent values will not overwrite the original values, but will be appended to the back. If only one array is assigned and the array is indexed by number, the key name is re-indexed continuously. Example:
Output result: array ([0] => A [1] => 4 [2] => 9 [3] => 3 [4] => K [5] => 7 [6] => 5 [7] => Q [8] => 6 [9] => 8 [10] => 2 [11] => J) 2. recursively append an array. the array_merge_recursive () function is the same as the array_merge () function. you can combine two or more numbers to form a combined array. The difference between the two is that when a key in an input array already exists in the result array, the function will adopt different processing methods. array_merge () overwrites the existing key/value pairs and replaces them with the key/value pairs in the current input array. array_merge_recursive () merges the two values, form a new array and use the original key as the array name. There is also a number combination form, that is, recursively append an array. The format is array array_merge_recursive (array key, array values) Example:
100, "James" => 85); $ class2 = array ("Micky" => 78, "John" => 45); $ classScores = array_merge_recursive ($ class1, $ class2); print_r ($ classScores);?> Output result: Array ([John] => Array ([0] => 100 [1] => 45) [James] => 85 [Micky] => 78) 3. connect two arrays to the array_combine () function to get a new array consisting of a set of submitted keys and corresponding values. The format is: array array_merge (array array1, array array2 [..., Array arrayN]) Note: the two input arrays must be of the same size and cannot be empty. Example:
Output result: Array ([AL] => Alabama [AK] => Alaska [AZ] => Arizona [AR] => Arkansas) This section describes how to merge, append, and connect arrays in php. I hope this will help you. Programmer's house. I wish you all a better learning experience. |