PHP中可以使用以下方法进行集合的交集、并集和差集运算:
array_intersect()
函数:$array1 = [1, 2, 3, 4];
$array2 = [2, 4, 6, 8];
$intersection = array_intersect($array1, $array2);
print_r($intersection); // Output: Array ( [1] => 2 [3] => 4 )
array_merge()
函数:$array1 = [1, 2, 3, 4];
$array2 = [2, 4, 6, 8];
$union = array_merge($array1, $array2);
$union = array_unique($union); // 去除重复元素
print_r($union); // Output: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [5] => 6 [6] => 8 )
array_diff()
函数:$array1 = [1, 2, 3, 4];
$array2 = [2, 4, 6, 8];
$difference = array_diff($array1, $array2);
print_r($difference); // Output: Array ( [0] => 1 [2] => 3 )