phparraysfiltercountintersection

Determine if two arrays have at least 3 shared values


My $source variable contains an array of numbers, in fact, they are id's of the products currently placed in shopping cart, so the array could have different count of values.

for example:

$source = [34, 22, 25, 64, 60];

My $find variable contains a set of numbers which I'm looking for in the $source array, there will be for example 12 numbers.

$find = [1,2,3,4,5,6,7,8,9,10,11,12];

I need to apply this logic:

Build an IF statement to check if the $source array contains at least 3 values from $find array.


Solution

  • if (count(array_intersect($source, $find)) >= 3)
    

    http://php.net/array_intersect