Php two array intersection difference set array_intersect -- calculate the array intersection description arrayarray_intersect (arrayarray1, arrayarray2 [, array...]) array_intersect () returns an array containing all values that appear in the array of all other parameters at the same time in array1. Note that the key names are not reserved.
Array_intersect -- describe the intersection of arrays
Array array_intersect (array array1, array array2 [, array...])
Array_intersect () returns an array containing all values that appear in the array of all other parameters at the same time in array1. Note that the key name remains unchanged.
Example 1. array_intersect () example
$ Array1 = array ("a" => "green", "red", "blue ");
$ Array2 = array ("B" => "green", "yellow", "red ");
$ Result = array_intersect ($ array1, $ array2 );
?>
This makes $ result:
Array
(
[A] => green
[0] => red
)
Array_diff -- describe the difference set of the calculated array
Array array_diff (array array1, array array2 [, array...])
Array_diff () returns an array containing all values in the array of array1 but not in any other parameter. Note that the key name remains unchanged.
Example 1. array_diff () example
$ Array1 = array ("a" => "green", "red", "blue", "red ");
$ Array2 = array ("B" => "green", "yellow", "red ");
$ Result = array_diff ($ array1, $ array2 );
?>
This makes $ result an array (1 => "blue ");. Values that appear multiple times in $ array1 are treated as the same.
Note: The two units are considered the same only when (string) $ elem1 === (string) $ elem2. That is to say, when the expression of the string is the same.
Note: This function only checks one dimension in a multi-dimensional array. Of course, you can use array_diff ($ array1 [0], $ array2 [0]); to check deeper dimensions.
Array_diff_assoc -- calculate the array's difference set with index check
Description
Array array_diff_assoc (array array1, array array2 [, array...])
Array_diff_assoc () returns an array that contains all values in the array of array1 but not in any other parameter. Note that, unlike array_diff (), the key name is also used for comparison.
Example 1. array_diff_assoc () example
$ Array1 = array ("a" => "green", "B" => "brown", "c" => "blue", "red ");
$ Array2 = array ("a" => "green", "yellow", "red ");
$ Result = array_diff_assoc ($ array1, $ array2 );
/* The result is:
Array
(
[B] => brown
[C] => blue
[0] => red
)
*/
?>
In the above example, we can see that the key-value pair "a" => "green" exists in both arrays, so it is not in the output of this function. In contrast, the key-value pair 0 => "red" appears in the output because the key name of "red" in the second parameter is 1.
The two values in key => value are considered equal only when (string) $ elem1 === (string) $ elem2. That is to say, the use of strict check, the expression of the string must be the same.