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
    )
3
  • 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() may help. Commented Nov 18, 2015 at 16:09
  • From where you got this array Commented Nov 18, 2015 at 16:14
  • I believe this is awnsered here Commented Nov 18, 2015 at 16:38

2 Answers 2

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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.