Compare the lengths arrays that have duplicates
I wish to determine whether there are any duplicates in two arrays, i.e. duplicates in array1 or duplicates in array2. If there are, then set a variable to equal 1, otherwise 0. I have the following code but it does not seem to work and I cannot understand why:
$a = count(array_unique($myarraydf));
$b = count($myarraydf);
$c = count(array_unique($myarrayds));
$d = count($myarrayds);
if (($a == $b) || ($c == $d)) {
$ties = 0;
}
else {
$ties = 1;
}
开发者_高级运维
where $myarraydf and $myarrayds are arrays of numeric values.
If you want to set $ties = 1 if there are duplicates in either set, you need to change your operator to AND
:
if (($a == $b) and ($c == $d)) {
If you want to set $ties = 1 if both contain duplicates, then the OR
is correct.
I suggest using array_diff
, see: http://us3.php.net/array_diff
精彩评论