Skip to content
Advertisement

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)

the function should delete all unique values and 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. Furthermore I am sure there is a more elegant and efficient way to do this. Thank you for your help in advance.

Advertisement

Answer

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]; }
));
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement