Page 1 of 1

Array Comparison

Posted: Tue Mar 29, 2011 8:20 pm
by neesley
How does one compare two arrays?

For example--I have two arrays. I would like a simple true/false as to whether any values from array 1 appear in array 2. in_array will only search for a single value no? A loop would work I'm sure, but there has to be a more elegant way...

Thoughts? Thanks.

Re: Array Comparison

Posted: Wed Mar 30, 2011 12:18 am
by KeithK
You might be looking for array_diff().

Re: Array Comparison

Posted: Wed Mar 30, 2011 2:14 pm
by neesley
I did have that in mind. What does array_diff() return if it finds nothing in common among arrays? Thanks.

Re: Array Comparison

Posted: Wed Mar 30, 2011 2:18 pm
by AbraCadaver

Code: Select all

if(array_intersect($array2, $array1)) {
If there are common values then it will return an array with the common values which will evaluate to true. If not it will return an empty array which evaluates to false.