Differences between the addition of array_merge and array in PHP. First, let's take a look at the key name is string, the difference between the two: Copy the code as follows :? Php $ arr1array (aPHP); $ arr2array (aJAVA); if the key name is a character with the same key name, ar first checks whether the key name is string:
The code is as follows:
$ Arr1 = array ('a' => 'php ');
$ Arr2 = array ('a' => 'Java ');
// If the key name is a character with the same key name, the array element value after array_merge () overwrites the previous array element value
Print_r (array_merge ($ arr1, $ arr2); // Array ([a] => JAVA)
// If the key name is a character with the same key name, the first value displayed in the array is used as the result.
Print_r ($ arr1 + $ arr2); // Array ([a] => PHP)
?>
If the key name is a number, the difference is as follows:
The code is as follows:
$ Arr1 = array ("C", "PHP ");
$ Arr2 = array ("JAVA", "PHP ");
// If the key name is a number, array_merge () will not overwrite
Print_r (array_merge ($ arr1, $ arr2 )); // Array ([0] => C [1] => PHP [2] => JAVA [3] => PHP)
// If the key name is an array, the first value in the array will be used as the result, and the values with the same key name will be discarded.
Print_r ($ arr1 + $ arr2); // Array ([0] => C [1] => PHP)
?>
Pay special attention to this. The difference between "array_merge and array addition" is clearly described above, back to my initial question: "How can we get two sets of array values with the same character key name and different values most effectively, it is not commonly used for obtaining: array_merge_recursive-recursively merges one or more arrays. if the input array contains the same string key name, these values will be merged into an array. For example:
The code is as follows:
$ Arr1 = array ("a" => "php", "c ");
$ Arr2 = array ("a" => "java", "c", "ruby ");
Print_r (array_merge_recursive ($ arr1, $ arr2 ));
?>
The result is as follows:
Array
(
[A] => Array
(
[0] => php
[1] => java
)
[0] => c
[1] => c
[2] => ruby
)
In this way, you can obtain a set of element values with the same names in multiple arrays.
The pipeline code is as follows :? Php $ arr1 = array ('a' = 'php'); $ arr2 = array ('a' = 'Java'); // if the key name is a character, and the key name is the same, ar...