检查一个数组的元素是否在 PHP 中的另一个数组中

检查一个数组的元素是否在 PHP 中的另一个数组中

问题描述:

我在 PHP 中有两个数组,如下所示:

I have two arrays in PHP as follows:

人:

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

通缉犯:

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
)

如何检查任何 People 元素是否在通缉犯 数组中?

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

在这个例子中,它应该返回 true 因为 20通缉犯中.

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

您可以使用 array_intersect().

You can use array_intersect().

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