php - 检查数组中是否存在任何值

标签 php arrays

想缩短这段代码

if( (in_array('2', $values)) or (in_array('5', $values)) or (in_array('6', $values)) or (in_array('8', $values)) ){
echo 'contains 2 or 5 or 6 or 8';
}

试过了

(in_array(array('2', '5', '6', '8'), $values, true))

但据我所知,只有当所有值都存在于数组中时才为真

求教

最佳答案

尝试 array_intersect() , 例如

if (count(array_intersect($values, array('2', '5', '6', '8'))) > 0) {
    echo 'contains 2 or 5 or 6 or 8';
}

此处示例 - http://codepad.viper-7.com/GFiLGx

关于php - 检查数组中是否存在任何值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17585627/

相关文章:

php - 如何从table1中选择还没有付款条目的记录?

php - 无法在 docker 中挂载卷

php - 通过 PayPal 进行全局捐赠

php - 使用 PHP、MYSQL、GROUP_CONCAT 和 JOIN 进行空间搜索

python - numpy不同形状的两个数组之间的组合

php - 严格标准:非静态方法 STemplate::assign() 不应静态调用

Java,需要帮助创建一个方法来选择数组中的特定元素

c - 以 float 形式打印数组,其中大小取决于用户输入

java - 将字符串子集转换为数组变量的最佳方法是什么?

arrays - 在 O(log n) 中搜索未排序数组中的值