2

I have an array, last two elements are identical, i just want to check duplicate exist or not.

Array
(
    [0] => Array
        (
            [crop] => CI-000001
            [type] => PT-000001
        )

    [1] => Array
        (
            [crop] => CI-000001
            [type] => PT-000003
        )

    [2] => Array
        (
            [crop] => CI-000005
            [type] => PT-000014
        )

    [3] => Array
        (
            [crop] => CI-000005
            [type] => PT-000014
        )

) 
John Conde
  • 217,595
  • 99
  • 455
  • 496
Jon Rulz
  • 21
  • 6
  • 1
    You're probably not the first one asking this, besides you have shown no effort of trying. – Daan May 20 '15 at 13:34

3 Answers3

0

try like this

<?php
$array = array(array('crop' => 'CI-000001','type' => 'PT-000001'), array('crop' => 'CI-000001','type' => 'PT-000003'),array('crop' => 'CI-000005','type' => 'PT-000014'),array('crop' => 'CI-000005','type' => 'PT-000014'));
$array = array_map("unserialize", array_unique(array_map("serialize", $array)));
echo "After Remove Duplicate:".'<pre>';
print_r( $array );
echo '</pre>';
?>

Output:-

After Remove Duplicate:
Array
(
    [0] => Array
        (
            [crop] => CI-000001
            [type] => PT-000001
        )

    [1] => Array
        (
            [crop] => CI-000001
            [type] => PT-000003
        )

    [2] => Array
        (
            [crop] => CI-000005
            [type] => PT-000014
        )

)

Demo

RaMeSh
  • 3,330
  • 2
  • 19
  • 31
0

Try the following code:

$hashes=array();
foreach ($myarray as $key=>$item) {
  $hash=sha1(var_export($item, true));
  if (isset($hashes($hash)) echo "$key is a duplicate of ".$hashes[$hash];
  else $hashes[$hash]=$key;
}
kenorb
  • 155,785
  • 88
  • 678
  • 743
Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92
0

You need to use array_unique function of PHP as

$ara  = Array ( Array ( 'crop' => 'CI-000001', 'type' => 'PT-000001' ), Array
    (
        'crop' => 'CI-000001',
        'type' => 'PT-000003'
    ), Array
    (
        'crop' => 'CI-000005',
        'type' => 'PT-000014'
    ), Array
    (
        'crop' => 'CI-000005',
        'type' => 'PT-000014'
    )
);
echo "<pre>";
print_r(array_unique($ara,SORT_REGULAR));
echo "</pre>";

Output:

Array
(
    [0] => Array
        (
            [crop] => CI-000001
            [type] => PT-000001
        )

    [1] => Array
        (
            [crop] => CI-000001
            [type] => PT-000003
        )

    [2] => Array
        (
            [crop] => CI-000005
            [type] => PT-000014
        )

)
Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54