查找具有重复值的数组并比较它们的键 [英] Find arrays with duplicate value and compare their key
问题描述
您好,我想找到具有相同值key EAN 的数组。如果存在多个记录,我想删除key ProductCount 值较低的记录。
例如我有一个数组。如你所见,有三个记录相同的ean ($ arr [0],$ arr [1],$ a [3])。在找到具有相同ean的这些记录后,我需要比较其关键ProductCount的值,并将记录与ProductCount的最小值保持一致。你了解吗?
我会感谢任何建议。
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 thesame 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
));
我的尝试:
我试过这个以及对此的一些修改但没有成功。我试图这样做几天:(
What I have tried:
I tried this and some modifications of this but without success. I am trying to do it for days :(
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;
}
推荐答案
arr [0],
arr [1],
[3])。在找到具有相同ean的这些记录后,我需要比较其关键ProductCount的值,并将记录与ProductCount的最小值保持一致。你了解吗?
我会感谢任何建议。
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.
这篇关于查找具有重复值的数组并比较它们的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!