PHP Return Only Duplicated Entries from an Array

php return only duplicated entries from an array

You will need to make your function case insensitive to get the "Hello" => "hello" result you are looking for, try this method:

$arr = array(1=>'1233',2=>'12334',3 =>'Hello' ,4=>'hello', 5=>'U');

// Convert every value to uppercase, and remove duplicate values
$withoutDuplicates = array_unique(array_map("strtoupper", $arr));

// The difference in the original array, and the $withoutDuplicates array
// will be the duplicate values
$duplicates = array_diff($arr, $withoutDuplicates);
print_r($duplicates);

Output is:

Array
(
[3] => Hello
[4] => hello
)

Edit by @AlixAxel:

This answer is very misleading. It only works in this specific condition. This counter-example:

$arr = array(1=>'1233',2=>'12334',3 =>'Hello' ,4=>'HELLO', 5=>'U');

Fails miserably. Also, this is not the way to keep duplicates:

array_diff($arr, array_unique($arr));

Since one of the duplicated values will be in array_unique, and then chopped off by array_diff.

Edit by @RyanDay:

So look at @Srikanth's or @Bucabay's answer, which work for all cases (look for case insensitive in Bucabay's), not just the test data specified in the question.

Check for duplicate entries in array but ONLY return values that are duplicated

Depending on what you want displayed (for example this just gives the ID of a row) it would be better to try it in SQL...

SELECT ID 
FROM `barcodes`
GROUP BY ID
HAVING count(ID)>1

The idea is to just retrieve ID's where the count value is more than one.

So this could look like...

$query = mysqli_query($dbc, "SELECT barcode 
FROM `barcodes`
GROUP BY barcode
HAVING count(barcode)>1");

$result_array = mysqli_fetch_all($query, MYSQLI_ASSOC);

print_r($result_array);

If you wanted to have the number of times it exists, just add the count to the SELECT list...

SELECT ID, count(ID) times
FROM `barcodes`
GROUP BY ID
HAVING count(ID)>1

Get an array of duplicate array values

$arr = array('a','a','b','c','d','d','e');
$arr_unique = array_unique($arr);
$arr_duplicates = array_diff_assoc($arr, $arr_unique);
print_r($arr_duplicates);

The above will return

Array
(
[1] => a
[5] => d
)

How to get only duplicate values from array

Just for fun, here it is in just a few lines:

$values = array(
array('1:30am', '2:00am', '3:00am'),
array('2:30am', '5:00am', '6:30am'),
array('2:00am', '3:00am', '5:00am')
);

$flatten = array();
array_walk_recursive($values, function ($item) use (&$flatten) {$flatten[]=$item;});
$duplicates = array_keys(array_filter(array_count_values($flatten),function($item){return $item>1;}));

print_r($duplicates);

Of course, readability is about zero.

The main takeaway is that you have to work with one dimensional arrays to use most of PHP's array functions. So somehow you have to flatten your multi-dimensional array first.

Show only duplicate elements from an array

Use this code:

# assuming your original array is $arr
array_unique(array_diff_assoc($arr, array_unique($arr)));

It will return unique duplicates but if you want non-unique duplicates then use:

array_diff_assoc($arr, array_unique($arr));

EDIT: Based on your comments, try this code:

$uarr = array_unique($arr);
var_dump(array_diff($arr, array_diff($uarr, array_diff_assoc($arr, $uarr))));

OUTPUT

array(4) {
[5]=>
string(5) "Ahmed"
[6]=>
string(5) "Ahmed"
[7]=>
string(4) "Ajay"
[8]=>
string(4) "Ajay"
}

Check and return duplicates array php

this will be ~100 times faster than array_diff

$dups = array();
foreach(array_count_values($arr) as $val => $c)
if($c > 1) $dups[] = $val;

How to show only the non duplicated values of an array

<?php
$original = array("red", "red", "blue");
$unique = array_unique($original);
print_r($original);
print_r($unique);
?>

How to remove duplicate values from an array in PHP

Use array_unique().

Example:

$array = array(1, 2, 2, 3);
$array = array_unique($array); // Array is now (1, 2, 3)


Related Topics



Leave a reply



Submit