Link to home
Start Free TrialLog in
Avatar of johnkolbert
johnkolbert

asked on

Comparing Two Arrays with PHP

Is there a way to do the following:

Compare the elements of two arrays and see if any element in one matches any element in the other (the arrays may be different lengths).

For instance:

Array 1 contains:
red
green
blue
balck
white

Array 2 contains:
pink
purple
blue

The resulting expression would show "true" since arrays 1 and 2 both have blue.

Is this possible?
ASKER CERTIFIED SOLUTION
Avatar of wildzero
wildzero

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wildzero
wildzero

You can even modify that do to

$a1 = array('red','green','blue','black','white');
$a2 = array('pink','purple','blue');

$a3 = array_intersect($a1,$a2);
If (count($a3) > 0) {
  echo "Has Some Items";
  print_r($ar); //this will show the matching items.
} else {
  echo "No matching items";
}
Avatar of johnkolbert

ASKER

Ah, perfect. That seems to be what I'm looking for! Many thanks!
print_r($ar); //this will show the matching items.

should be

print_r($a3); //this will show the matching items.

:-)
Glade it helped.