This article mainly introduces two methods of php array merging, and also introduces the difference between using the plus sign and using array_merge to merge arrays. For more information, see the following.
The code is as follows:
$ R = array (1, 2, 3, 4, 5, 6 );
$ E = array (7,8, 9, 10 );
?>
Here we use the array_merge and the plus sign to add these two arrays.
The code is as follows:
Print_r ($ r + e ); // output Array ([0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6)
Print"
";
Print_r (array_merge ($ r, $ e )); // output Array ([0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9)
?>
It can be seen from this that the value in an array is combined with array_merge and appended to the value behind the previous array. If the array contains the number key name, the subsequent values will not overwrite the original values, but will be appended to the back. However, the plus sign is used to merge arrays. if the key name is the same, the first array value is used.
Next we will change the array given above
The code is as follows:
$ R = array ('R' => 1, 2, 3, 4, 5, 6 );
$ E = array ('R' => 7, 8, 9, 10 );
?>
The code is as follows:
Print_r ($ r + e ); // output Array ([r] => 1 [0] => 2 [1] => 3 [2] => 4 [3] => 5 [4] => 6)
Print"
";
Print_r (array_merge ($ r, $ e )); // output Array ([0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9)
?>
It can be seen from this that the value in an array is combined with array_merge and appended to the value behind the previous array. If the names of non-numeric keys are the same, the values of the subsequent arrays overwrite the values of the preceding arrays. However, the plus sign is used to merge arrays. if the key name is the same, the first array value is used.