0

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?

1
  • did u tried array_diff() Commented May 16, 2019 at 15:43

1 Answer 1

3

You can use array_intersect_assoc

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

Live Demo

Sign up to request clarification or add additional context in comments.

Comments

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.