How to Detect Duplicate Values in PHP Array

How to detect duplicate values in PHP array?

You can use array_count_values function

$array = array('apple', 'orange', 'pear', 'banana', 'apple',
'pear', 'kiwi', 'kiwi', 'kiwi');

print_r(array_count_values($array));

will output

Array
(
[apple] => 2
[orange] => 1
[pear] => 2
etc...
)

How do I find duplicates in a PHP array?

One option is using array_count_values() and include only array elements with more than one values.

$cars = array("Volvo", "BMW", "Toyota", "BMW", "Toyota");

foreach( array_count_values($cars) as $key => $val ) {
if ( $val > 1 ) $result[] = $key; //Push the key to the array sice the value is more than 1
}

echo "<pre>";
print_r( $result );
echo "</pre>";

This will result to:

Array
(
[0] => BMW
[1] => Toyota
)

Doc: array_count_values()

How can we find the Duplicate values in array using php?

Try this : this will give you the count of each value

$array = array("192.168.1.1", "192.168.2.1","192.168.3.1","192.168.4.1","192.168.2.1","192.168.2.1","192.168.10.1","192.168.2.1","192.168.11.1") ;

$cnt_array = array_count_values($array)

echo "<pre>";
print_r($cnt_array);

$res = array();
foreach($cnt_array as $key=>$val){
if($val == 1){
$res[$key] = 'unique';
}
else{
$res[$key] = 'duplicate';
}
}

echo "<pre>";
print_r($res);

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)

How to detect duplicate values in multidimensional associative array?

Simple solution is:

<?php

$data = [
[
'name' => 'name 1',
'phone' => '12341234',
'email' => 'test@web.com'
],
[
'name' => 'name 2',
'phone' => '12341234',
'email' => 'test@web1.com'
],
[
'name' => 'name 3',
'phone' => '4322342',
'email' => 'test@web1.com'
],
[
'name' => 'name 4',
'phone' => '1234123423',
'email' => 'test@web1.com'
],
[
'name' => 'name 5',
'phone' => '12341266634',
'email' => 'test@eqweqwweb.com'
],
];

$phones = [];
$emails = [];
foreach ($data as $key => $contact) {
if (array_search($contact['phone'], $phones) !== false || array_search($contact['email'], $emails) !== false) {
unset($data[$key]);
} else {
$phones[] = $contact['phone'];
$emails[] = $contact['email'];
}
}

var_dump($data);

and in result you'll get:

array(3) {
[0] =>
array(3) {
'name' =>
string(6) "name 1"
'phone' =>
string(8) "12341234"
'email' =>
string(12) "test@web.com"
}
[2] =>
array(3) {
'name' =>
string(6) "name 3"
'phone' =>
string(7) "4322342"
'email' =>
string(13) "test@web1.com"
}
[4] =>
array(3) {
'name' =>
string(6) "name 5"
'phone' =>
string(11) "12341266634"
'email' =>
string(18) "test@eqweqwweb.com"
}
}

this is just example.

php: check if an array has duplicates

You can do:

function has_dupes($array) {
$dupe_array = array();
foreach ($array as $val) {
if (++$dupe_array[$val] > 1) {
return true;
}
}
return false;
}

php check array value for duplicate

Using array_unique(), this can be easily refactored into a new function:

function array_is_unique($array) {
return array_unique($array) == $array;
}

Example:

$array = array("a", "a", "b", "c");
echo array_is_unique($array) ? "unique" : "non-unique"; //"non-unique"

PHP - Check for duplicate values in an associative array

One option to check if an array has duplicates is to get the count of unique values. If it does not match the count of the original array, then there are duplicates.

$arr = array('Email','Name','Something','Else','Email');

if ( count( $arr ) !== count( array_unique( $arr ) ) ) echo "Some duplicates";

Doc: array_unique()

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


Related Topics



Leave a reply



Submit