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

Michael Berkowski
  • 253,311
  • 39
  • 421
  • 371
spsaravananct
  • 362
  • 2
  • 4
  • 16

1 Answers1

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
}
AbraCadaver
  • 73,820
  • 7
  • 55
  • 81
Danijel
  • 11,490
  • 4
  • 32
  • 51