如果指针是数组,如何使用in_array ?

时间:2021-03-09 22:49:43

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);

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:

我要做的是检查$arr1中的所有值是否存在于$arr2中。上面的例子应该是正确的:

$arr3 = array(1,2,4,5,6,7);

comparing $arr1 with $arr3 will return a FALSE.

将$arr1与$arr3进行比较将返回一个FALSE。

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.

通常我使用in_array,因为我只需要将单个值检查到数组中。但是在本例中,in_array不能使用。我想看看是否有一种简单的方法用最小循环来进行检查。

UPDATE for clarification.

更新澄清。

First array will be a set that contains unique values. Second array can contain duplicated values. They are both guaranteed an array before processing.

第一个数组将是一个包含唯一值的集合。第二个数组可以包含重复的值。它们在处理之前都被保证是一个数组。

4 个解决方案

#1


61  

Use array_diff():

使用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
}

#2


27  

You can use array_intersect or array_diff:

可以使用array_intersect或array_diff:

$arr1 = array(1,2,3);
$arr2 = array(1,2,3,4,5,6,7);

if ( $arr1 == array_intersect($arr1, $arr2) ) {
    // All elements of arr1 are in arr2
}

However, if you don't need to use the result of the intersection (which seems to be your case), it is more space and time efficient to use array_diff:

但是,如果您不需要使用交集的结果(这似乎是您的情况),那么使用array_diff更节省空间和时间:

$arr1 = array(1,2,3);
$arr2 = array(1,2,3,4,5,6,7);
$diff = array_diff($arr1, $arr2);

if ( empty($diff) ) {
    // All elements of arr1 are in arr2
}

#3


5  

You can try use the array_diff() function to find the difference between the two arrays, this might help you. I think to clarify you mean, all the values in the first array must be in the second array, but not the other way around.

您可以尝试使用array_diff()函数来查找这两个数组之间的区别,这可能会对您有所帮助。我想澄清一下你的意思,第一个数组中的所有值必须在第二个数组中,而不是反过来。

#4


0  

In my particular case I needed to check if a pair of ids was processed before or not. So simple array_diff() did not work for me.

在我的例子中,我需要检查一对id是否在之前被处理过。所以简单的array_diff()对我不起作用。

Instead I generated keys from ids sorted alphabetically and used them with in_array:

相反,我从按字母顺序排序的id中生成键,并使用in_array:

<?php
$pairs = array();
// ...
$pair = array($currentId, $id);
sort($pair);
$pair = implode('-', $pair);
if (in_array($pair, $pairs)) {
    continue;
}
$pairs[$pair] = $pair;

This is probably not an optimum solution at all but I just needed it for a dirty script to be executed once.

这可能根本不是最佳的解决方案,但我只是需要它来执行一次脏脚本。

#1


61  

Use array_diff():

使用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
}

#2


27  

You can use array_intersect or array_diff:

可以使用array_intersect或array_diff:

$arr1 = array(1,2,3);
$arr2 = array(1,2,3,4,5,6,7);

if ( $arr1 == array_intersect($arr1, $arr2) ) {
    // All elements of arr1 are in arr2
}

However, if you don't need to use the result of the intersection (which seems to be your case), it is more space and time efficient to use array_diff:

但是,如果您不需要使用交集的结果(这似乎是您的情况),那么使用array_diff更节省空间和时间:

$arr1 = array(1,2,3);
$arr2 = array(1,2,3,4,5,6,7);
$diff = array_diff($arr1, $arr2);

if ( empty($diff) ) {
    // All elements of arr1 are in arr2
}

#3


5  

You can try use the array_diff() function to find the difference between the two arrays, this might help you. I think to clarify you mean, all the values in the first array must be in the second array, but not the other way around.

您可以尝试使用array_diff()函数来查找这两个数组之间的区别,这可能会对您有所帮助。我想澄清一下你的意思,第一个数组中的所有值必须在第二个数组中,而不是反过来。

#4


0  

In my particular case I needed to check if a pair of ids was processed before or not. So simple array_diff() did not work for me.

在我的例子中,我需要检查一对id是否在之前被处理过。所以简单的array_diff()对我不起作用。

Instead I generated keys from ids sorted alphabetically and used them with in_array:

相反,我从按字母顺序排序的id中生成键,并使用in_array:

<?php
$pairs = array();
// ...
$pair = array($currentId, $id);
sort($pair);
$pair = implode('-', $pair);
if (in_array($pair, $pairs)) {
    continue;
}
$pairs[$pair] = $pair;

This is probably not an optimum solution at all but I just needed it for a dirty script to be executed once.

这可能根本不是最佳的解决方案,但我只是需要它来执行一次脏脚本。