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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
should be
print_r($a3); //this will show the matching items.
:-)
Glade it helped.
$a1 = array('red','green','blue'
$a2 = array('pink','purple','blu
$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";
}