The array_merge () function combines two or more numbers into an array.
If the key name already exists, the key value of the key is the value corresponding to the last key name (the subsequent one overwrites the previous one ). If the array is a digital index, the key name is re-indexed continuously.
Note: If you only input an array to the array_merge () function and the key name is an integer, the function returns a new array with an integer key name, its key name starts from 0 and is re-indexed.
The Code is as follows: |
Copy code |
<? Php $ A = array (3 => "Horse", 4 => "Dog "); Print_r (array_merge ($ )); ?>
|
Combine the elements of one or more arrays. The values of 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:
The Code is as follows: |
Copy code |
$ 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,
The Code is as follows: |
Copy code |
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) |
Recursive append 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, 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:
The Code is as follows: |
Copy code |
<? Php $ Array1 = array ("AA" => "A", "BB" => "B", "CC" => "C ", "DD" => "D "); $ Array2 = array ("AA" => "1", "BB" => "2", "CC" => "3 ", "DD" => "4 "); $ Array3 = array ("AA" => "! "," BB "=>" @ "," CC "=>" # "," DD "=>" $ "); $ ArrayX = array_merge ($ array1, $ array2, $ array3); // merge Arrays Print_r ($ arrayX ); ?> |
Connect two Arrays
The array_combine () function returns 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:
The Code is as follows: |
Copy code |
<? Php $ Array1 = array ("AA" => "A", "BB" => "B", "CC" => "C ", "DD" => "D "); $ Array2 = array ("AA" => "1", "BB" => "2", "CC" => "3 ", "DD" => "4 "); $ Array3 = array ("AA" => "! "," BB "=>" @ "," CC "=>" # "," DD "=>" $ "); $ ArrayX = array_merge ($ array1, $ array2, $ array3); // merge Arrays Print_r ($ arrayX ); ?> |