更新时间:2023-08-19 18:14:52
尝试:
使用array_filter()
从数组中获取所有重复值
Use array_filter()
to get all duplicate values from array
使用array_diff()
从数组中获取所有唯一值
Use array_diff()
to get all unique values from array
$array = array(1=>0, 2=>1, 3=>1, 4=>2);
$counts = array_count_values($array);
$duplicates = array_filter($array, function ($value) use ($counts) {
return $counts[$value] > 1;
});
print '<pre>';print_r($duplicates);
$result=array_diff($array,$duplicates);
print '<pre>';print_r($result);
输出:
Array
(
[2] => 1
[3] => 1
)
Array
(
[1] => 0
[4] => 2
)