Find arrays with duplicate value and compare their keys value

There are duplicate EANs, for example - 6900532615069, 6900532655058, 6909536620208, 6909536620369, …

Their ID’s are different?

Well, what I posted worked for the test cases I used. Try it out.

One more question, what are all the options you have available to export data out of your accounting software?

Hrabosh, Did these two lines not work for you?

It does not work. In this case It always keep the first record with duplicate ean even if the record has a bigger productCount.

I do not have any options. I must work with the xml file.

Yes, ID’s are different but it is the same as ProductCount… You can sort by productCount or ID but you always need a record with the smallest ProductCount so I think It is better to sort it by ProductCount.

It looks like your code is working. I am going to try it.

I thought you wanted the lowest productCount? If you want the highest, use this:
Well, you need to sort the data by productCount.
Then, get the first unique ean.

function removeduplicateKeys($data){
array_multisort( array_column($data, “productCount”), SORT_DESC, $data );
$tempArr = array_unique(array_column($data, “ean”));
return array_intersect_key($data, $tempArr);
}

Yes, I wanted the lowest ProductCount. Maybe I wrote it wrong, sorry.

Here is your code https://www.ideone.com/XIgF87. If you can see $arr[5] has the smallest ProductCount than $arr[0] (Both has the same ean) but in the output is the $arr[0]… That’s wrong. Do you understand?

I will try the code from you last post. I think your last code can be used with SORT_ASC.

Yes, I changed it to ASC and it shows the lowest. Here is the test code I used:
— <?php

function removeduplicateKeys($data){
    array_multisort( array_column($data, 'productCount'), SORT_ASC, $data );
    $tempArr = array_unique(array_column($data, 'ean'));
    return array_intersect_key($data, $tempArr);
}
 
$arr =array( 
"0" => Array
(
    "ean" => 6900532615069,
    "productPrice" => 1140,
    "productCount" => 10,
),	
"1" => Array
(
    "ean" => 6900532615069,
    "productPrice" => 1140,
    "productCount" => 50
),
"2" => Array
(
    "ean" => 6900535364122,
    "productPrice" => 1140,
    "productCount" => 50
),
"3" => Array
(
    "ean" => 6900535364122,
    "productPrice" => 1140,
    "productCount" => 500
),
"4" => Array
(
    "ean" => 6900535364122,
    "productPrice" => 1140,
    "productCount" => 0
),
"5" => Array
(
    "ean" => 6900532615069,
    "productPrice" => 1140,
    "productCount" => 5,
));
 
print_r(removeduplicateKeys($arr));

Hope this works for you…

2 Likes
Sponsor our Newsletter | Privacy Policy | Terms of Service