tags:

views:

77

answers:

1

I have two arrays...I want to find the common element in the array.I want to find the unique values in array1 and unique values in array2.... Example:

Array1:"red,blue,green,violet"

Array2:"yellow,orange,violet,blue

Now i want to know how will i retrive

uniq_Arr1=>red,green

uniq_Arr2=>yellow,orange

common_Arr=>violet,blue

That is,From each array it sholud retrive the unique elements and the common elements... Please guide me to know this...

+10  A: 

Use array_intersect() to find common elements and array_diff() to find the differences.

Here's my test code:

$array1 = array("red", "blue", "green", "violet");
$array2 = array("yellow", "orange", "violet", "blue");

$uniq_arr1 = array_diff($array1, $array2);
$uniq_arr2 = array_diff($array2, $array1);
$common_arr = array_intersect($array1, $array2);
yjerem
Thanks..Just now i tried.....Can anyone explain me the same in two dimensional array,so that i could know still better.....
Sreeja
Not much more to be said, I think.
Tchalvak
Play with the code for a bit, you will begin to understand how it works.
Shane Reustle
array_diff is broken in php, simple loop code will be ~100 times faster than that. See http://stackoverflow.com/questions/2479963/how-does-array-diff-work/2480131#2480131
stereofrog