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 )