note 102582 deleted from function.array-diff by danbrown
[email protected] Wed, 23 Feb 2011 10:56:20 -0800
| Newsgroups | php.notes |
|---|---|
| Message-ID | <[email protected]> |
Note Submitter: anton
----
A function to find values that only occur once in a number of arrays. I still need one to allow any amount of input arrays, so if anyone knows, please let me know.
This is my function to find all the unique values:
<?php
function array_uniquevalues($array1,$array2,$array3)
{
$trvalues[] = array_merge(
array_intersect($array1,$array2),array_intersect($array2,$array1)
);
$trvalues[] = array_merge(
array_intersect($array2,$array3),array_intersect($array3,$array2)
);
$trvalues[] = array_merge(
array_intersect($array1,$array3),array_intersect($array3,$array1)
);
$trvalues = array_unique(
array_merge($trvalues[0],$trvalues[1],$trvalues[2])
);
$tvalues = array_unique(array_merge($array1,$array2,$array3));
$rvalues = array_diff($tvalues,$trvalues);
$rvalues = array_values($rvalues);
return $rvalues;
}
$array1 = array('red','green','blue');
$array2 = array('red','yellow','green');
$array3 = array('pink','yellow','red');
print_re(array_uniquevalues($array1,$array2,$array3));
?>
result:
Array
(
[0]=>blue
[1]=>pink
)