Home > Article > Backend Development > PHP array merge + difference analysis with array_merge and deduplication techniques for merging multiple arrays
You can use + or array_merge to merge two arrays in PHP, but there are still differences between them. This article introduces the difference analysis between PHP array merge + and array_merge and the deduplication techniques for merging multiple arrays. If necessary Friends can read this article.
The main difference is that if the same key name appears in two or more arrays, the key name is divided into a string or a number. Please note that
1) The key name is Number, the value after array_merge() will not overwrite the original value, but will be appended to it. However, +merging the array will return the first value as the final result, and the subsequent arrays with the same key name will be returned. Those values are "discarded" (not overwritten)
2) When the key name is string, array_merge() will overwrite the previous value with the same key name, but + will still The value that appears first is returned as the final result, and those values in subsequent arrays with the same key name are "discarded" (not overwritten).
It should be noted that the array key form 'number' is equivalent to a number
$a = array('a', 'b'); $b = array('c', 'd'); $c = $a + $b; var_dump($a); var_dump(array_merge($a, $b)); //输出: array 0 => string 'a' (length=1) 1 => string 'b' (length=1) array 0 => string 'a' (length=1) 1 => string 'b' (length=1) 2 => string 'c' (length=1) 3 => string 'd' (length=1) ++++++++++++++++++++++++++++++++++++++++++ $a = array( 0 => 'a', 1 => 'b' ); $b = array( 0 => 'c', 1 => 'b' ); $c = $a + $b; var_dump($c); var_dump(array_merge($a, $b)); //输出: array 0 => string 'a' (length=1) 1 => string 'b' (length=1) array 0 => string 'a' (length=1) 1 => string 'b' (length=1) 2 => string 'c' (length=1) 3 => string 'b' (length=1) ++++++++++++++++++++++++++++++++++++++++++ $a = array('a', 'b'); $b = array( '0' => 'c', 1 => 'b' ); $c = $a + $b; var_dump($c); var_dump(array_merge($a, $b)); //输出: array 0 => string 'a' (length=1) 1 => string 'b' (length=1) array 0 => string 'a' (length=1) 1 => string 'b' (length=1) 2 => string 'c' (length=1) 3 => string 'b' (length=1) ++++++++++++++++++++++++++++++++++++++++++ $a = array( 0 => 'a', 1 => 'b' ); $b = array( '0' => 'c', '1' => 'b' ); $c = $a + $b; var_dump($c); var_dump(array_merge($a, $b)); 输出: array 0 => string 'a' (length=1) 1 => string 'b' (length=1) array 0 => string 'a' (length=1) 1 => string 'b' (length=1) 2 => string 'c' (length=1) 3 => string 'b' (length=1)
Merge and remove duplicates from multiple arrays
$a = array('1001','1002'); $b = array('1002','1003','1004'); $c = array('1003','1004','1005'); $d = count(array_flip($a) + array_flip($b) + array_flip($c));
Related recommendations:
PHP method of merging strings or arrays into one array
Two ways to merge two-dimensional arrays in PHP
Introduction to several methods of php array merging
The above is PHP array merging+ Analysis of the difference with array_merge and detailed information on the techniques for merging multiple arrays to remove duplicates. For more information, please pay attention to other related articles on the PHP Chinese website!
The above is the detailed content of PHP array merge + difference analysis with array_merge and deduplication techniques for merging multiple arrays. For more information, please follow other related articles on the PHP Chinese website!