[英]PHP: Delete unique values from an array
I would like to delete all unique values from an array.我想从数组中删除所有唯一值。
So lets say I have $array = (1,2,3,5,4,3,4,5,234)
所以假设我有
$array = (1,2,3,5,4,3,4,5,234)
the function should delete all unique values and output: function 应该删除所有唯一值和 output:
$newarray = (3,5,4,3,4,5)
I just thought of a solution with array_count_values
but I do not know how I could iterate it.我只是想到了一个带有
array_count_values
的解决方案,但我不知道如何迭代它。 Furthermore I am sure there is a more elegant and efficient way to do this.此外,我确信有一种更优雅、更有效的方法来做到这一点。 Thank you for your help in advance.
提前谢谢你的帮助。
One of solutions:解决方案之一:
$array = [1,2,3,5,4,3,4,5,234];
$freq = array_count_values($array);
print_r(array_filter(
$array,
function ($v) use ($freq) { return 1 < $freq[$v]; }
));
$array = (1,2,3,5,4,3,4,5,234);
$freq = array_count_values($array);
$output = array();
foreach($array as $val){
if($freq[$val] >1){
$output[] = $val;
}
}
print_r($output);
May be not the most efficient way.可能不是最有效的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.