I need to test if one element of an array is in another array.
我需要测试一个数组的一个元素是否在另一个数组中。
$array_one = array("gogo", "blabla", "toto");
$array_two = array("stackov", "renaul", "toto");
I would like to know if one element of array_one
is in array_two
???
我想知道array_one的一个元素是否在array_two中?
How to test that? Am trying in_array
but it seems to have problems.
怎么测试?我在尝试in_array,但似乎有问题。
3 个解决方案
#1
18
array_intersect()
$array1 = array("gogo", "blabla", "toto");
$array2 = array("stackov","renaul","toto");
$commonElements = array_intersect($array1,$array2);
var_dump($commonElements);
#2
3
Try this one:
试试这个:
array_intersect($array_one, $array_two);
#3
2
Mark's answer should be enough for your problem. If you ever wish to find the intersect of more than 2 arrays, use this:
马克的答案应该足以解决你的问题。如果您希望找到两个以上数组的交叉,请使用:
$arrays = array(
array(1, 2, 3),
array(2, 4, 6),
array(2, 8, 16)
);
$intersection = call_user_func_array('array_intersect', $arrays);
#1
18
array_intersect()
$array1 = array("gogo", "blabla", "toto");
$array2 = array("stackov","renaul","toto");
$commonElements = array_intersect($array1,$array2);
var_dump($commonElements);
#2
3
Try this one:
试试这个:
array_intersect($array_one, $array_two);
#3
2
Mark's answer should be enough for your problem. If you ever wish to find the intersect of more than 2 arrays, use this:
马克的答案应该足以解决你的问题。如果您希望找到两个以上数组的交叉,请使用:
$arrays = array(
array(1, 2, 3),
array(2, 4, 6),
array(2, 8, 16)
);
$intersection = call_user_func_array('array_intersect', $arrays);