Find arrays with duplicate value and compare their keys value

Hello, I would like to find arrays with same value of key EAN . If multiple records exist, I want to remove the record with lower value of key ProductCount .

For example I have an array. As you can see there are three records with the same ean ($arr[0], $arr[1], $a[3]). After finding these records with the same ean I need to compare their values of key ProductCount and keep the record with the smallest value of ProductCount. Do you understand?

I would be thankful for any advice.

$arr =array( 
"0" => Array
(
    "ean" => 6900532615069,
    "productPrice" => 1140,
    "productCount" => 5
),
"1" => Array
(
    "ean" => 6900532615069,
    "productPrice" => 1140,
    "productCount" => 50
),
"2" => Array
(
    "ean" => 6900535364122,
    "productPrice" => 1140,
    "productCount" => 50
),

"3" => Array
(
    "ean" => 6900532615069,
    "productPrice" => 1140,
    "productCount" => 10,
));

So output should be

$arr =array( 
"0" => Array
    (
        "ean" => 6900532615069,
        "productPrice" => 1140,
        "productCount" => 5
    ),
"1" => Array
    (
        "ean" => 6900535364122,
        "productPrice" => 1140,
        "productCount" => 50
    ));

I try this and some modifications of this but without any success.

function removeduplicateKeys($data){

$_data = array();

foreach ($data as $v) {
  if (isset($_data[$v['ean']])) {
    // found duplicate
    continue;
  }
  // remember unique item
  $_data[$v['ean']] = $v;
}

$data = array_values($_data);
return $data;

}

Thank you

I’m confused.

Isn’t that what you said the output should be?

Yes, It is but it’s just a coincidence. If you try this https://www.ideone.com/0FtwoP, you can see the output is wrong. The output should be

Array
(
[0] => Array
    (
        [ean] => 6900532615069
        [productPrice] => 1140
        [productCount] => 10
    )

[1] => Array
    (
        [ean] => 6900535364122
        [productPrice] => 1140
        [productCount] => 50
    )

)

Where does this array come from in the first place? I smell an XY problem.

1 Like

Before I work on a sorting algorithm, is this coming from a database?

It comes from xml file. I have xml file (generated by accounting software) which has products and array is created from xml file.

There is link to original and complete array which I has http://b2b.pneuvratimov.cz/src/cron.php?action=test

Duplicate products by ean are:

$arr[2][‘ean’] = $arr[37][‘ean’]
$arr[3][‘ean’] = $arr[38][‘ean’]
$arr[4][‘ean’] = $arr[39][‘ean’]
$arr[10][‘ean’] = $arr[40][‘ean’]
$arr[20][‘ean’] = $arr[36][‘ean’]

I tried some for cycles, nested foreachs and so on but I’m lost.

No, look at my last post.

Fixed the logic errors and tested against various values.

You already have the array. Just run a multidimensional sort of it on the ean and price-count.
Then, you can just loop thru the array again and the first entry for each ean will be the lowest.
PHP.net array-multisort

Updated the last fiddle.

Sorry, @astonecipher, I missed your fiddle. Thought it was an advert. LOL

But, can’t you just use array_multisort and then array_unique and pull out the first lowest one for each key?
Wouldn’t that be faster?

OP, can you please post the XML Source file.

You can use this function:

function removeduplicates($data){
$tempArr = array_unique(array_column($data, ‘ean’));
return array_intersect_key($data, $tempArr);
}

Much much faster! Simple sort and intersect… I wouldn’t even put it into a function since it is so fast…

@ErnieAlex, the above is with your snippet. It has the same issue my original did, it missed values.

The XML would be helpful to see if there is another way to go about this. There may be another attribute that can be used to eliminate nodes off the bat. Of course, there are other languages where this would be even easier!

Thus my call on an XY Problem…

Okay, but it does solve the issue in two lines… Give a fix for the XY issues!

I am going to try all your advices. Here is xml file http://b2b.pneuvratimov.cz/import-respone.xml and the code which converts xml into products array. https://www.ideone.com/zaymxJ

That isn’t XML that I see, it is just values.

Sorry, link edited. Look at my last post.

Based on that, I am only seeing a single instance of each EAN. Are there files where there are more?

Sponsor our Newsletter | Privacy Policy | Terms of Service