How to Show Only Duplicate Values From an Array With PHP

By | December 3, 2013

If you have an array with duplicate values, sometimes you might need to show only non-unique elements. Here is a short code in PHP that will allow you to do this.

<?
$arr=array_map("trim", file("file.txt"));
$nonuniq=array_unique(array_diff_assoc($arr, array_unique($arr)));
?>

This will return unique array of duplicates. If you need to show all duplicated values, use this code.

<?
$arr=array_map("trim", file("file.txt"));
$nonuniq=array_diff_assoc($arr, array_unique($arr));
?>

Leave a Reply

Your email address will not be published. Required fields are marked *