首页 > 解决方案 > 使用 array_intersect 检查数组中的多个值

问题描述

我有这两个数组和两种方法。$inputArray 可以是 only[1,2],也可以是 [1,2,3,4]。

$inputArray = [1,2];
$inputArray = [1,2,3,4];
$mainArray=[1,2,3,4,6,7,9];

$testObj->method1();

$testObj->method2();

现在只需要在数组中没有 [1,2] 时执行方法 1。

我尝试了类似的方法,但 $inputArray 失败了。

if( count( $mainArray ) == count( array_intersect( $mainArray, $inputArray ) ) ) {
    $testObj->method1();
} else {
    $testObj->method2();
}

这是伪代码。

标签: php

解决方案


试用in_array()功能:

if(!in_array("1", $inputArray) && !in_array("2", $inputArray)) {
  $testObj->method1();
}

推荐阅读