The intersection of two Arrays Using array_intersect is faster than the union of the two Arrays Using array_diff.
If the number of difference sets between array $ a and array $ B is required, use count ($ a)-count (array_intersect ($ a, $ B )), instead of using count (array_diff ($ a, $ B ));
The front is faster than the latter, and more obvious in the large array.
The array_intersect () function returns the intersection array of two or more arrays.
The result array contains all values in the compared array and all other parameter arrays. The key name remains unchanged.
Note: Only values are used for comparison.
Syntax
Array_intersect (array1, array2, array3.) parameter description
Array1 is required. The first array to be compared with other arrays.
Array2 is required. The array to be compared with the first array.
Array3 is optional. The array to be compared with the first array. There can be multiple.
Example
$ A1 = array (0 => "Cat", 1 => "Dog", 2 => "Horse ");
$ A2 = array (3 => "Horse", 4 => "Dog", 5 => "Fish ");
Print_r (array_diff ($ a1, $ a2 ));
?> Output:
Array ([0] => Cat)