0
$categories => Array([0] => Jewelry & Accessories,[1] => Pet Care)
$categories_master => Array([0] => Jewelry & Accessories,[1] => Apparel,[2] => Beauty & Fragrance)

I have two arrays like above,

I have to check like this in_array($categories,$categories_master), I know it wont work but I need to return 1 or true for anyone match with $categories_master array

10
  • Can you better describe the output you want to receive? array_intersect() may be very close to what you need. Commented Dec 10, 2014 at 19:54
  • I need true or false like in_array output Commented Dec 10, 2014 at 19:55
  • At least one item match with master category Commented Dec 10, 2014 at 19:56
  • So if any one item matches, return true? Use array_intersect() and verify that the count() >= 1 Commented Dec 10, 2014 at 19:57
  • 1
    To avoid confusion, can you please specify what the output should be for your example above? Should it be true or array(true, false) Commented Dec 10, 2014 at 19:59

1 Answer 1

2

Do the array_intersect ( returns an array containing all the values of $categories that are present in $categories_master ), and cast to boolean. If return array has items it will return true, or false otherwise.

(bool) array_intersect( $categories, $categories_master );

Also this will evaluate to true if the returned array is not empty, false for empty array:

if(array_intersect( $categories, $categories_master )) {
    //there are one or more matches
}
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.