Combine multiple numbers. 1. Combine the array. the array_merge () function merges the array and returns a Union array. the obtained array starts with the first input array parameter and then follows the array.
Combine multiple numbers
1. Merge arrays
The array_merge () function combines the arrays and returns a Union array. the obtained array starts with the first input array parameter and is added sequentially according to the order in which the following array parameters appear, the format is:
- Array array_merge (array array1 array2 ..., Arrayn)
Combine the elements of one or more arrays. the values of an array are appended to the previous array and returned as an array of results.
If the input array contains the same string key name, the value after the key name will overwrite the previous value. However, if the array contains the number key name, the subsequent values will not overwrite the original values, but will be appended to the subsequent values.
If only one array is assigned and the array is digitally indexed, the key name is re-indexed in a continuous manner. The example is as follows:
- $ Face = array ("j", "q", "k", "");
- $ Numbered = array ("2", "3", "4", "5", "6", "7", "8", "9 ");
- $ Cards = array_merge ($ face, $ numbered );
- Shuffle ($ cards );
- Print_r ($ cards );
This will return the following results:
- 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. two or more numbers can be combined 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, this function takes different processing methods. array_merge () overwrites the previous key/value pair, replace it with the key/value pair in the current input array, while array_merge_recursive () merges the two values to form a new array and uses the original key as the array name. Another form of array merging is recursively appending an array, in the form:
- Array array_merge_recursive (array key, array values)
Example:
- $ Class1 = array ("john" => 100, "james" => 85 );
- $ Class2 = array ("micky" => 78, "john" => 45 );
- $ Classscores = array_merge_recursive ($ class1, $ class2 );
- Print_r ($ classscores );
The following result is returned:
- Array ([john] => array ([0] => 100 [1] => 45) [james] => 85 [micky] => 78)
3. connect two arrays
The array_combine () function returns a new array consisting of a group of submitted keys and corresponding values. The form is:
- Array array_merge (array array1, array array2 [..., Array arrayn])
Note: the two input arrays must be of the same size and cannot be blank. the instance is as follows:
- $ Abbreviations = array ("al", "ak", "az", "ar ");
- $ States = array ("alabama", "alaska", "arizona", "arkansas ");
- $ Statemap = array_combine ($ abbreviations, $ states );
- Print_r ($ statemap );
This will return:
- Array ([al] => alabama [ak] => alaska [az] => arizona [ar] => arkansas)