I need a advice from you. I have an array and I need to find arrays with same value of key. Then I need to compare another key of founded arrays and remove array which has lower value of key.
Example below.
As you can see, there are two arrays with same EAN key. I need to find arrays with same EAN. Then compare these two array by key ProductCount. The array with a higer ProdouctCount should be removed. Do you understand?
[20] => Array
(
[ean] => **6900532615069**
[productPrice] => 1140
[productCount] => 50
)
[25] => Array
(
[ean] => 6900535364122
[productPrice] => 1140
[productCount] => 50
)
[36] => Array
(
[ean] => **6900532615069**
[productPrice] => 1140
[productCount] => 10
)
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;
}
So output should be
[25] => Array
(
[ean] => 6900535364122
[productPrice] => 1140
[productCount] => 50
)
[36] => Array
(
[ean] => **6900532615069**
[productPrice] => 1140
[productCount] => 10
)
I am trying to do it for about three days but I do not how. The farthest thing I did was deleting a duplicate array but I don't know how to compare a key value and then delete the array. I would be grateful for any advice. Thank you.