Checking if ANY of an array's elements are in another array Ask Question

Checking if ANY of an array's elements are in another array Ask Question

I have two arrays in PHP as follows:

People:

Array
(
    [0] => 3
    [1] => 20
)

Wanted Criminals:

Array
(
    [0] => 2
    [1] => 4
    [2] => 8
    [3] => 11
    [4] => 12
    [5] => 13
    [6] => 14
    [7] => 15
    [8] => 16
    [9] => 17
    [10] => 18
    [11] => 19
    [12] => 20
)

How do I check if any of the People elements are in the Wanted Criminals array?

In this example, it should return true because 20 is in Wanted Criminals.

ベストアンサー1

You can use array_intersect().

$peopleContainsCriminal = !empty(array_intersect($people, $criminals));

おすすめ記事