用于array_search()
在数组中搜索给定值:
$a = array(0 => '1,3');
$b = array(
0 => '1,2,4',
1 => '1,2',
2 => '4,3',
3 => '2,4',
4 => '1,3'
);
foreach ($a as $val) {
if ($key = array_search($val, $b)) {
echo "'$val' is matched in '$key' index";
break;
}
}
输出:
'1,3' is matched in '4' index
您还可以执行以下操作:
$match = array();
foreach ($a as $val) {
if (array_search($val, $b)) {
$match[] = $val;
}
}
print_r($match);
输出:
Array
(
[0] => 1,3
)
更新:
正如OP所提到的,为此我们也使用array_intersect()
函数:
$a = array(0 => '1,3');
$b = array(
0 => '1,2,4',
1 => '1,2',
2 => '4,3',
3 => '2,4',
4 => '1,3'
);
print_r(array_intersect($a, $b));
输出:
Array
(
[0] => 1,3
)