If you want the array to $a the number of difference sets $b to the array, you should use COUNT ($a)-count (Array_intersect ($a, $b) instead of Count (Array_diff ($a, $b));
The front is faster than the latter, and is more pronounced in large arrays.
1.array_intersect function
Array Array_intersect (array $array 1, array $array 2 [, array $ ...])
Array_intersect () returns an array that contains all the values in Array1 that also appear in all other parameter arrays. Note that the key name remains unchanged.
#1 Array_intersect () example
Copy Code code as follows:
<?php
$array 1 = Array ("A" => "green", "Red", "blue");
$array 2 = Array ("B" => "green", "yellow", "red");
$result = Array_intersect ($array 1, $array 2);
?>
This makes $result become:
Array
(
[A] => green
[0] => Red
)
2. Self-Implementation array_intersect () function is five times times faster than PHP original function array_intersect ()
Copy Code code as follows:
/**
*
* Custom Array_intersect
* If we ask for the intersection of one-dimensional arrays, this function is 5 times times faster than the array_intersect of the system.
*
* @param array $arr 1
* @param array $arr 2
* @author Liubotao 2010-12-13 11:40:20
*
*/
function My_array_intersect ($arr 1, $arr 2)
{
For ($i =0 $i <sizeof ($arr 1); $i + +)
{
$temp []= $arr 1[$i];
}
For ($i =0 $i <sizeof ($arr 1); $i + +)
{
$temp []= $arr 2[$i];
}
Sort ($temp);
$get =array ();
for ($i =0; $i <sizeof ($temp); $i + +)
{
if ($temp [$i]== $temp [$i +1])
$get []= $temp [$i];
}
return $get;
}
$array 1 = Array ("Green", "Red", "blue");
$array 2 = Array ("green", "yellow", "red");
echo "<pre>";
Print_r (My_array_intersect ($array 1, $array 2));
echo "<pre/>";
array_diff-Calculating the difference set of an array
Array Array_diff (array $array 1, array $array 2 [, array $ ...])
Array_diff () returns an array that includes all values that are in array1 but not in any other parameter array. Note that the key name remains unchanged.
#1 Array_diff () example
Copy Code code as follows:
<?php
$array 1 = Array ("A" => "green", "Red", "Blue", "Red");
$array 2 = Array ("B" => "green", "yellow", "red");
$result = Array_diff ($array 1, $array 2);
Print_r ($result);
?>
Processed in the same way as a value that occurs more than once in $array 1, the output is:
Copy Code code as follows:
Note: Two units are considered identical only in (string) $elem 1 = = (string) $elem 2 o'clock. In other words, when the string expression is the same.
Note: Notice that this function examines only one dimension of a multidimensional array. Certainly can use Array_diff ($array 1[0], $array 2[0]); Check for deeper dimensions.