[英]php arrays - in_array and/or array_intersect
今天又变胖了,很抱歉。 :)无论如何,我有两个要操纵的数组; 如果第二个数组中存在第一个数组中的值,则先做一件事,然后再对第二个数组的其余值做其他事情。
例如
$array1 = array('1','2','3','4'); - the needle
$array2 = array('1','3','5','7'); - the haystack
if(in_array($array1,$array2): echo 'the needle'; else: echo'the haystack LESS the needle '; endif;
但是由于某种原因, in_array
对我不起作用。 请帮助。
像这样做:
<?php
$array1 = array('1','2','3','4');
$array2 = array('1','3','5','7');
//case 1:
print_r(array_intersect($array1, $array2));
//case 2:
print_r(array_diff($array2, $array1));
?>
这将输出array的值(在更改问题之前您想要的值):
Array
(
[0] => 1
[2] => 3
)
Array
(
[2] => 5
[3] => 7
)
并且,如果您想使用if-else
,请按照以下步骤进行操作:
<?php
$array1 = array('1','2','3','4');
$array2 = array('1','3','5','7');
$intesect = array_intersect($array1, $array2);
if(count($intesect))
{
echo 'the needle';
print_r($intesect);
}
else
{
echo'the haystack LESS the needle ';
print_r(array_diff($array2, $array1));
}
?>
输出:
the needle
Array
(
[0] => 1
[2] => 3
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.