检查多个值是否存在 php 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15515560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:23:24  来源:igfitidea点击:

check multiple values exists php array

phparrays

提问by Red

I have an array in PHP

我在 PHP 中有一个数组

$permission = array( "admin", "moderator", "guest"  );

and i have another array

我还有另一个数组

$userRoles = array( "admin", "moderator" );

I checked with in_arraybut it doesn't work with multiple values.

我检查过,in_array但它不适用于多个值。

How can i check atleast one value in $userRolesexists on $permissionwithout looping ?

如何$userRoles$permission不循环的情况下检查存在的至少一个值?

Thanks in advance.

提前致谢。

回答by Explosion Pills

Use array_intersect

array_intersect

count(array_intersect($permission, $userRoles));

回答by Dipesh Parmar

Use array_intersect

array_intersect

array_intersect— Computes the intersection of arrays

array array_intersect ( array $array1 , array $array2 [, array $ ... ] )

array_intersect()returns an array containing all the values of array1 that are present in all the arguments. Note that keys are preserved.

array_intersect— 计算数组的交集

大批 array_intersect ( array $array1 , array $array2 [, array $ ... ] )

array_intersect()返回一个数组,其中包含所有参数中出现的 array1 的所有值。请注意,密钥被保留。

Read

Read