要返回唯一元素(那些在任何一个数组中只存在一次但在两个数组中都不存在的元素),请尝试
function array_unique_diff ($array1, $array2)
{
array_merge(array_diff_key($array1, $array2), array_diff_key($array2, $array1));
}
范例
$array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);
array_diff_key($array1, $array2)
返回
array ( 'red' => 2, 'purple' => 4 )
array_diff_key($array2, $array1)
返回
array ( 'yellow' => 7, 'cyan' => 8, )
array_unique_diff($array1, $array2);
返回
array ( 'red' => 2, 'purple' => 4, 'yellow' => 7, 'cyan' => 8, )