1. Merging arrays
The Array_merge () function merges the arrays together, returning a federated array. The resulting array begins with the first input array parameter and is forced in the order in which the following array parameters appear. In the form of:
Copy Code code as follows:
Array Array_merge (array array1 array2...,arrayn)
Merges one or more arrays of cells, and the values in an array are appended to the previous array. Returns an array as the result.
If the input array has the same string key name, the value following the key name overrides the previous value. However, if the array contains a numeric key name, the subsequent value will not overwrite the original value, but append to the back.
If only one array is given and the array is a numeric index, the key name is sequentially indexed.
Examples are as follows:
Copy Code code as follows:
$face = Array ("J", "Q", "K", "A");
$numbered = Array ("2", "3", "4", "5", "6", "7", "8", "9");
$cards = Array_merge ($face, $numbered);
Shuffle ($cards);
Print_r ($cards);
This returns the results shown below, running the code:
Copy Code code as follows:
Array ([0] => A [1] => 4 [2] => 9 [3] => 3 [4] => K [5] => 7 [6] => 5 [7] => Q [8] => 6 [9] =& Gt 8 [Ten] => 2 [one] => J)
2. Recursive append array
The array_merge_recursive () function is the same as the Array_merge () and can combine two or more numbers together to form a federated array. The difference between the two is that when a key in an input array is already in the result array, the function takes a different approach. Array_merge () overrides the previously existing key/value pairs, replacing the key/value pairs in the current input array, and array_merge_recursive () merges two values together to form a new array with the original key as the array name. There is also the form of an array merge, which is to append the array recursively. In the form of:
View Sourceprint?array array_merge_recursive (array key,array values)
Examples are as follows:
Copy Code code as follows:
$class 1 = Array ("John" =>, "James" => 85);
$class 2 = Array ("Micky" =>, "John" => 45);
$classScores = array_merge_recursive ($class 1, $class 2);
Print_r ($classScores);
This will return the following results:
View Sourceprint? Array ([John] => Array ([0] => [1] =>) [James] => [Micky] => 78)
3. Connect two arrays
The Array_combine () function gets a new array that consists of a set of committed keys and corresponding values. In the form of:
View Sourceprint?array array_merge (array array1,array array2[...,array Arrayn])
Note that two input arrays must be of the same size and cannot be empty. Examples are as follows:
Copy Code code 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:
Copy Code code as follows:
Array ([AL] => Alabama [AK] => Alaska [AZ] => Arizona [AR] => Arkansas)