fightstarr20
fightstarr20

Reputation: 12618

PHP use in_array to check array against another array

I am trying to check if any values out or array $a1 are present in array $a2 in PHP...

$a1 = array(
    "a"=>"red",
    "b"=>"green",
    "c"=>"blue",
    "d"=>"yellow"
);

$a2 = array(
    "b"=>"green",
    "c"=>"blue",
);

I have tried to compare using in_array like this...

if (in_array($a1, $a2)) {
  echo "Match found";
}

But this is not working, I think this is because in_array does not support checking an array against an array. What is the correct method?

Upvotes: 0

Views: 35

Answers (1)

Rakesh Jakhar
Rakesh Jakhar

Reputation: 6388

You can use array_intersect_assoc

$res =array_intersect_assoc($a1, $a2);

Live Demo

Upvotes: 3

Related Questions