简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM