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
  • 267,341
  • 46
  • 444
  • 390
spsaravananct
  • 392
  • 2
  • 4
  • 17

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
  • 78,200
  • 7
  • 66
  • 87
Danijel
  • 12,408
  • 5
  • 38
  • 54