0

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...

0

1 Answer 1

10

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);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks..Just now i tried.....Can anyone explain me the same in two dimensional array,so that i could know still better.....
Not much more to be said, I think.
Play with the code for a bit, you will begin to understand how it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.