检查一个数组的元素是否在 PHP 中的另一个数组中 [英] Checking to see if one array's elements are in another array in PHP

查看:22
本文介绍了检查一个数组的元素是否在 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));

这篇关于检查一个数组的元素是否在 PHP 中的另一个数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆