时间:2021-07-01 10:21:17 帮助过:5人阅读
$array = array("red"=>"Red","green"=>"red4","Red15"=>"Red",7=>"Level","Width"=>"Red","azzzz1"=>"art","peak"=>158);
$array1 = array("red"=>"Red2","greena"=>"red","Red15"=>"Red",7=>"Level","Width"=>"Red","azzzz"=>"art","peak"=>158);
$num = array_intersect($array,$array1);
print_r ($num);
echo "
";
$num = array_intersect_assoc($array,$array1);
print_r($num);
echo "
";
$num = array_intersect_key($array,$array1);
print_r ($num);
?>
运行结果:
代码如下:
Array ( [red] => Red [Red15] => Red [7] => Level [Width] => Red [azzzz1] => art [peak] => 158 )
Array ( [Red15] => Red [7] => Level [Width] => Red [peak] => 158 )
Array ( [red] => Red [Red15] => Red [7] => Level [Width] => Red [peak] => 158 )
总结:
1.array_intersect()函数进行的比较只有数组值的比较,而且存在如”Red“和”Red2“比较时会返回"Red",反之则不会返回"Red2";
2.array_intersect_assoc()函数是将数组的值与键值一起比较,而且不会存在array_intersect的情况,适用于较严格的比较;
3.array_intersect_key()函数适用于比较两个数组键值的交集,返回的并不只有键值,而是键值和对应的数组值。