php判断两个数组是否存在交集的三种情况:
1.array_intersect() 比较两个数组的值,并返回交集:
<?php$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");$a2=array("e"=>"red","f"=>"green","g"=>"blue"); $result=array_intersect($a1,$a2);print_r($result);?>结果: Array ( [a] => red [b] => green [c] => blue )
array_intersect_key() 比较两个数组的键名,并返回交集:
$a1=array("a"=>"red","b"=>"green","c"=>"blue");$a2=array("a"=>"red","c"=>"blue","d"=>"pink");$result=array_intersect_key($a1,$a2);print_r($result);?> 结果: Array ( [a] => red [c] => blue )
array_intersect_assoc() 比较两个数组的键名和键值,并返回交集:
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");$a2=array("a"=>"red","b"=>"green","c"=>"blue");$result=array_intersect_assoc($a1,$a2);print_r($result);?> 结果: Array ( [a] => red [b] => green [c] => blue )
最后使用使用===运算符判断交集数组是否空
来源地址:https://blog.csdn.net/weixin_43637581/article/details/127091588