0

How can I check and remove duplicates from the array based on ProductReference?

This is my array:

Array
 (
    [0] => Array
        (
            [Order] => 1
            [Name] => pro book 40X80
            [ProductReference] => FM7291
            [Description] => pro book
            [Product_type] => photobook
            [Pages] => 20
            [AdicRange] => 2
            [Width] => 40
            [Height] => 40
            [AvailableOption] => 0
            [PicPreviews] => Array
                (
                    [PicPreview] => Array
                        (
                            [UrlOriginal] => http://dnmyog93u35ow.cloudfront.net/assets/software/xl_1491302876209.png
                            [UrlThumb] => http://dnmyog93u35ow.cloudfront.net/assets/software/xs_1491302876209.png
                        )
                )
            [SubCategories] => Array
                (
                )
         )
       [1] => Array
        (
            [Order] => 1
            [Name] => pro book 40X80
            [ProductReference] => FM7291
            [Description] => pro book
            [Product_type] => photobook
            [Pages] => 20
            [AdicRange] => 2
            [Width] => 40
            [Height] => 40
            [AvailableOption] => 0
            [PicPreviews] => Array
                (
                    [PicPreview] => Array
                        (
                            [UrlOriginal] => http://dnmyog93u35ow.cloudfront.net/assets/software/xl_1491302876209.png
                            [UrlThumb] => http://dnmyog93u35ow.cloudfront.net/assets/software/xs_1491302876209.png
                        )
                )
            [SubCategories] => Array
                (
                )
        )
    [2] => Array
        (
            [Order] => 2
            [Name] => 21X29 booklet Ver
            [ProductReference] => DF0754(A4)
            [Description] => 29X21 booklet Ver
            [Product_type] => photobook
            [Pages] => 24
            [AdicRange] => 4
            [Width] => 20.3
            [Height] => 29.9
            [AvailableOption] => 0
            [PicPreviews] => Array
                (
                    [PicPreview] => Array
                        (
                            [UrlOriginal] => http://dnmyog93u35ow.cloudfront.net/assets/software/xl_1495636546424.jpg
                            [UrlThumb] => http://dnmyog93u35ow.cloudfront.net/assets/software/xs_1495636546424.jpg
                        )
                )
            [SubCategories] => Array
                (
                )
        )
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136

3 Answers3

3

This one-liner will overwrite earlier subarrays when a new duplicate subarray is found: (Demo)

$array=array_values(array_column($array,null,"ProductReference"));

This two-liner will retain earlier subarrays and discard later ones: (Demo)

$rekeyed=array_column(array_reverse($array),null,"ProductReference");
sort($rekeyed);

apokryfos has identified the shortest/sweetest method to retain earlier subarrays and remove later ones: (Demo)

$array=array_unique($array,SORT_REGULAR);
// if the keys are important wrap array_unique() with array_values()
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
2

Make the unique value your key:

$newArray = array();
foreach($oldArray as $product){
    $newArray[$product['ProductReference']] = $product;
}
verhie
  • 1,298
  • 1
  • 7
  • 7
1

This may solve your problem or guide you.You can remove duplicate based on any key.Yes if you can make in database query go for that, that will be better. Directly copied from php manual

<?php 
$details = array( 
    0 => array("id"=>"1", "name"=>"Mike",    "num"=>"9876543210"), 
    1 => array("id"=>"2", "name"=>"Carissa", "num"=>"08548596258"), 
    2 => array("id"=>"1", "name"=>"Mathew",  "num"=>"784581254"), 
); 

function unique_multidim_array($array, $key) { 
    $temp_array = array(); 
    $i = 0; 
    $key_array = array(); 

    foreach($array as $val) { 
        if (!in_array($val[$key], $key_array)) { 
            $key_array[$i] = $val[$key]; 
            $temp_array[$i] = $val; 
        } 
        $i++; 
    } 
    return $temp_array; 
} 

$details = unique_multidim_array($details,'id'); 
var_dump($details);

output

$details = array( 
    0 => array("id"=>"1","name"=>"Mike","num"=>"9876543210"), 
    1 => array("id"=>"2","name"=>"Carissa","num"=>"08548596258"), 
); 
Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105