1

I have searched and tried many different "remove duplicates from array" functions but none have worked out for my case. I'm trying to remove a specific duplicate from an array.

From below I would like to remove the duplicates of "PHASER 4600" .

[0] => Array
    (
        [id] => 1737
        [product_name] => PHASER 4200
        [certification_date] => 3/20/12
    )

[1] => Array
    (
        [id] => 1738
        [product_name] => PHASER 4600
        [certification_date] => 3/20/12
    )

[2] => Array
    (
        [id] => 1739
        [product_name] => PHASER 4600
        [certification_date] => 3/20/12
    )

[3] => Array
    (
        [id] => 1740
        [product_name] => PHASER 4700
        [certification_date] => 3/20/12
    )

[4] => Array
    (
        [id] => 1741
        [product_name] => PHASER 4800
        [certification_date] => 3/20/12
    )
davejal
  • 6,009
  • 10
  • 39
  • 82
rubberchicken
  • 1,270
  • 2
  • 21
  • 47
  • You say you've tried various things, but don't show your working. Please add some code so we can help. Wrt the question, [array_filter()](http://php.net/array_filter) may help. – Jonnix Nov 18 '15 at 16:09
  • From where you got this array – kannan Nov 18 '15 at 16:14
  • I believe this is awnsered [here](http://stackoverflow.com/questions/307674/how-to-remove-duplicate-values-from-a-multi-dimensional-array-in-php) – Mazaka Nov 18 '15 at 16:38

2 Answers2

1

You could put them into a new array and check as you put them in to see if it is a duplicate.

$newArray = array();

foreach ($oldArray as $old) {
    $found = false;

    foreach ($newArray as $new) {
        if ($new['product_name'] == $old['product_name']) {
            $found = true;
        }
    }

    if (!$found) {
        array_push($newArray, $old);
    }
}
Fluinc
  • 491
  • 2
  • 10
1

You can use this function:

function delete_duplicate_name(&$arr, $name){
    $found = false;
    foreach($arr as $key => $elm){
        if($elm['product_name'] == $name){
            if($found == true)
                unset($arr[$key]);
            else
                $found = true;
        }
    }
}
delete_duplicate_name($arr, 'PHASER 4600');
print_r($arr);
Pablo Digiani
  • 602
  • 5
  • 9