数组的并集、交集、差集array_intersect() 和 array_diff() array_merge()函数

array_intersect() 函数用于比较两个(或更多个)数组的键值,并返回交集

array_intersect(array1,array2,array3...);

<?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_diff() 函数用于比较两个(或更多个)数组的值,并返回差集。

array_diff(array1,array2,array3...);

<?php
	$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
	$a2=array("e"=>"red","f"=>"green","g"=>"blue");

	$result=array_diff($a1,$a2);
	print_r($result);
?>

Array ( [d] => yellow )

array_merge() 函数把一个或多个数组合并为一个数组。

array_merge(array1,array2,array3...)

<?php
	$a1=array("red","green");
	$a2=array("blue","yellow");
	print_r(array_merge($a1,$a2));
?>

Array ( [0] => red [1] => green [2] => blue [3] => yellow )
posted @ 2020-11-04 23:46  caibaotimes  阅读(457)  评论(0编辑  收藏  举报