php - 使用 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();
}
这是伪代码。
解决方案
试用in_array()
功能:
if(!in_array("1", $inputArray) && !in_array("2", $inputArray)) {
$testObj->method1();
}
推荐阅读
- node.js - 在 NodeJS 中接收 ZMQ 视频流
- unity3d - WebGL中的Unity3D播放器没有动画
- python - 当表已经存在时,使用 spark 数据框覆盖表失败
- javascript - 如何使用 debouncetime 防止 Ionic 4 中的多次点击?
- docker - 保存 docker 镜像并在 plesk 中运行
- php - 谷歌幻灯片 php,管理共享设置
- vue.js - 在子组件中重用父组件的方法
- php - Doctrine2:左连接中的 SUM
- r - 如何通过相同的行名但按顺序连接两个数据框?
- webassembly - 为什么需要 WebAssembly?