Check It Yeah, It's On

4Mar/111

PHP: Finding Duplicates in Array

Using array_diff_assoc() and array_unique() to find duplicate values in an array.

<?php
$fruitBasket = array(
  'apple',
  'banana',
  'cantaloupe',
  'banana',
  'apple'
);

$duplicates = array_diff_assoc($fruitBasket, array_unique($fruitBasket));

print_r($duplicates);
?>

Output:

Array
(
    [3] => banana
    [4] => apple
)
Filed under: Code Leave a comment
Comments (1) Trackbacks (0)
  1. This is cool! Good post!


Leave a comment

No trackbacks yet.