Php 如何删除阵列中的重复数据?
我有以下数组:Php 如何删除阵列中的重复数据?,php,arrays,Php,Arrays,我有以下数组: Array ( [0] => Array ( [Import] => Array ( [product_id] => 1 [id] => 1 [category_id] => 1 [amount] => 50
Array
(
[0] => Array
(
[Import] => Array
(
[product_id] => 1
[id] => 1
[category_id] => 1
[amount] => 50
[cost] => 8320
[paid] => 0
[comment] => transportation and others cost: 100
[created] => 2015-06-22 12:09:20
)
[0] => Array
(
[total_sell] => 6
)
)
[1] => Array
(
[Import] => Array
(
[product_id] => 2
[id] => 2
[category_id] => 2
[amount] => 15
[cost] => 3000
[paid] => 0
[comment] =>
[created] => 2015-06-22 12:10:36
)
[0] => Array
(
[total_sell] => 1
)
)
[2] => Array
(
[Import] => Array
(
[product_id] => 1
[id] => 3
[category_id] => 1
[amount] => 15
[cost] => 2000
[paid] => 0
[comment] =>
[created] => 2015-06-22 12:10:58
)
[0] => Array
(
[total_sell] => 6
)
)
[3] => Array
(
[Import] => Array
(
[product_id] => 1
[id] => 4
[category_id] => 1
[amount] => 50
[cost] => 8000
[paid] => 0
[comment] =>
[created] => 2015-06-23 01:10:10
)
[0] => Array
(
[total_sell] => 6
)
)
)
我想删除[Import][product\u id]
的重复条目。因此,我的预期结果是:
Array
(
[0] => Array
(
[Import] => Array
(
[product_id] => 1
[id] => 1
[category_id] => 1
[amount] => 50
[cost] => 8320
[paid] => 0
[comment] => transportation and others cost: 100
[created] => 2015-06-22 12:09:20
)
[0] => Array
(
[total_sell] => 6
)
)
[1] => Array
(
[Import] => Array
(
[product_id] => 2
[id] => 2
[category_id] => 2
[amount] => 15
[cost] => 3000
[paid] => 0
[comment] =>
[created] => 2015-06-22 12:10:36
)
[0] => Array
(
[total_sell] => 1
)
)
)
您是否会编写一个函数来过滤这种类型的数组并产生预期的结果。我已经在谷歌上搜索了两天,但运气不好。这是一个方便的单行程序,应该可以做到这一点:
$unique= array_map("unserialize", array_unique(array_map("serialize", $original)));
如果基础阵列不相同,则无法工作,在这种情况下,我认为您可以执行以下操作:
$unique = array_intersect_key($original ,
array_unique(
array_map(function($item) {
return $item['Import']['product_id'];
}, $original)
)
);
已测试:这是一个方便的单行程序,应该可以实现以下功能:
$unique= array_map("unserialize", array_unique(array_map("serialize", $original)));
如果基础阵列不相同,则无法工作,在这种情况下,我认为您可以执行以下操作:
$unique = array_intersect_key($original ,
array_unique(
array_map(function($item) {
return $item['Import']['product_id'];
}, $original)
)
);
已测试:这是一个快速和及时运行的方法。保留第一个遇到的产品Id,并忽略具有重复产品Id的条目
// Stable sort
sort($in);
// Reduce
$out = array_reduce($in, function(&$acc, &$item){
if($item['Import']['product_id'] !== @$acc[sizeof($acc)-1]['Import']['product_id']) {
$acc[] = $item;
}
return $acc;
}, []);
$out = [];
$hashTable = [];
foreach($in as $item) {
$pid = $item['Import']['product_id'];
if(!isset($hashTable[$pid])) {
$out[] = $item;
$hashTable[$pid] = true;
}
}
演示:
更新:这里有一个更好的线性时间算法,它使用快速“哈希表”查找实现与上面相同的功能。同样,保留第一个遇到的产品Id,并忽略相同Id的后续产品Id
// Stable sort
sort($in);
// Reduce
$out = array_reduce($in, function(&$acc, &$item){
if($item['Import']['product_id'] !== @$acc[sizeof($acc)-1]['Import']['product_id']) {
$acc[] = $item;
}
return $acc;
}, []);
$out = [];
$hashTable = [];
foreach($in as $item) {
$pid = $item['Import']['product_id'];
if(!isset($hashTable[$pid])) {
$out[] = $item;
$hashTable[$pid] = true;
}
}
演示:这是一个快速、及时的演示。保留第一个遇到的产品Id,并忽略具有重复产品Id的条目
// Stable sort
sort($in);
// Reduce
$out = array_reduce($in, function(&$acc, &$item){
if($item['Import']['product_id'] !== @$acc[sizeof($acc)-1]['Import']['product_id']) {
$acc[] = $item;
}
return $acc;
}, []);
$out = [];
$hashTable = [];
foreach($in as $item) {
$pid = $item['Import']['product_id'];
if(!isset($hashTable[$pid])) {
$out[] = $item;
$hashTable[$pid] = true;
}
}
演示:
更新:这里有一个更好的线性时间算法,它使用快速“哈希表”查找实现与上面相同的功能。同样,保留第一个遇到的产品Id,并忽略相同Id的后续产品Id
// Stable sort
sort($in);
// Reduce
$out = array_reduce($in, function(&$acc, &$item){
if($item['Import']['product_id'] !== @$acc[sizeof($acc)-1]['Import']['product_id']) {
$acc[] = $item;
}
return $acc;
}, []);
$out = [];
$hashTable = [];
foreach($in as $item) {
$pid = $item['Import']['product_id'];
if(!isset($hashTable[$pid])) {
$out[] = $item;
$hashTable[$pid] = true;
}
}
演示:当遇到半重复时,您希望在最终结果中保留哪个版本的
产品id
?看起来像是“第一次遇到”。是吗?是的。“首次遇到”将保留在最终结果中当遇到半重复项时,您希望在最终结果中保留哪个版本的product\u id
?看起来像是“第一次遇到”。是吗?是的。如果只有产品id
相同,但其他值不相同,则“首次遇到”将保留在最终结果中?(顺便说一句,我没有否决)$unique=array_-map(“非序列化”,array_-unique(array_-map(“序列化”),$old_-array));这对我不起作用。我们应该询问OP他如何决定在最终结果中保留哪个版本的product\u id
。看起来像是“第一次遇到”是的。如果只有产品id
相同,但其他值不相同,则“首次遇到”将保留在最终结果中?(顺便说一句,我没有否决)$unique=array_-map(“非序列化”,array_-unique(array_-map(“序列化”),$old_-array));这对我不起作用。我们应该询问OP他如何决定在最终结果中保留哪个版本的product\u id
。看起来像是“第一次遇到”是的。“第一次遇到”将保留在最终结果中