如何使用in_array如果针是一个数组? [英] How can I use in_array if the needle is an array?
问题描述
我有2个阵列,该值将从数据库加载,下面是一个例子:
I have 2 arrays, the value will be loaded from database, below is an example:
$arr1 = array(1,2,3);
$arr2 = array(1,2,3,4,5,6,7);
我想要做的是检查所有的 $ ARR1
值在 $ ARR2 存在code>。上面的例子应该是
TRUE
,而
What I want to do is to check if all the values in $arr1
exist in $arr2
. The above example should be a TRUE
while:
$arr3 = array(1,2,4,5,6,7);
比较 $ ARR1
与 $ ARR3
将返回 FALSE
。
通常情况下我使用 in_array
,因为我只需要检查单个值到一个数组。但在这种情况下, in_array
不能使用。我想看看是否有一个简单的方法做检查以最小的循环。
Normally I use in_array
because I only need to check single value into an array. But in this case, in_array
cannot be used. I'd like to see if there is a simple way to do the checking with a minimum looping.
更新澄清。
第一个数组将是一个集包含唯一的值。第二个数组可以包含重复值。处理之前,他们都保证一个数组。
First array will be a set that contains unique values. Second array can contain duplicated values. They are both guaranteed an array before processing.
推荐答案
使用 和array_diff()
:
$arr1 = array(1,2,3);
$arr2 = array(1,2,3,4,5,6,7);
$arr3 = array_diff($arr1, $arr2);
if (count($arr3) == 0) {
// all of $arr1 is in $arr2
}
这篇关于如何使用in_array如果针是一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!