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 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() 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
PHP: How to check if duplicate values in array and keep record of them?
You can do this to detect if you have duplicates
$mergedValues = [];
foreach ($outerArray as $id => $innerArray) {
$mergedValues = array_merge($mergedValues, $innerArray);
}
$uniqueValues = array_unique($mergedValues);
if (sizeof($uniqueValues) != sizeof($mergedValues)) {
echo 'You have duplicates!', PHP_EOL;
echo 'These are duplicates: ', implode(', ', array_unique(array_diff_assoc($mergedValues, $uniqueValues))), PHP_EOL;
}
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 outputArray
(
[apple] => 2
[orange] => 1
[pear] => 2
etc...
)
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"
Related Topics
PHP Readfile() and Large Files
Save Accents in MySQL Database
Usort Issue with Decimal Numbers
Check If the Current Date Is Between Two Dates + MySQL Select Query
How to Display HTML to the Browser Incrementally Over a Long Period of Time
Share Variables/Memory Between All PHP Processes
Customizing My-Account Addresses Fields in Woocommerce 3
Missing CSS File and Images After Url Rewrite
How Would You Transform a Pre-Existing Web App into a Multilingual One
Reverse Letters in Each Word of a String Without Using Native Splitting or Reversing Functions
Call to Undefined Function Curl_Init() - with Wamp
How to Force Ssl in Codeigniter
How to Add Filter or Hook for "Woocommerce_Add_To_Cart"
Use MySQL_Fetch_Array() with Foreach() Instead of While()
Echo 'String' While Every Long Loop Iteration (Flush() Not Working)