php 如果针是数组,如何使用 in_array ?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2304436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use in_array if the needle is an array?
提问by Donny Kurnia
I have 2 arrays, the value will be loaded from database, below is an example:
我有 2 个数组,该值将从数据库加载,下面是一个示例:
$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 valuesin $arr1exist in $arr2. The above example should be a TRUEwhile:
我想要做的是检查是否所有的值中$arr1的存在$arr2。上面的例子应该是TRUE一会儿:
$arr3 = array(1,2,4,5,6,7);
comparing $arr1with $arr3will return a FALSE.
比较$arr1有$arr3将返回FALSE。
Normally I use in_arraybecause I only need to check single value into an array. But in this case, in_arraycannot 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.
第一个数组将是一个包含唯一值的集合。第二个数组可以包含重复的值。在处理之前,它们都保证是一个数组。
回答by cletus
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
}
回答by Justin Johnson
You can use array_intersector 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
}
回答by Sam152
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()函数来查找两个数组之间的差异,这可能会对您有所帮助。我想澄清你的意思,第一个数组中的所有值都必须在第二个数组中,而不是相反。
回答by Ivan Karpan
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.
这可能根本不是最佳解决方案,但我只需要它来执行一次脏脚本。

