我在php中设置了数组。我需要从下面的代码中删除重复/包含记录。
Array (
[0] => Array ( [percent] => 88.461538461538 [basenum] => 079 [code] => HL [group] => 0 )
[1] => Array ( [percent] => 88.461538461538 [basenum] => 097 [code] => HL [group] => 0 )
[2] => Array ( [percent] => 88.461538461538 [basenum] => 709 [code] => HL [group] => 0 )
[3] => Array ( [percent] => 88.461538461538 [basenum] => 790 [code] => HL [group] => 0 )
[4] => Array ( [percent] => 88.461538461538 [basenum] => 907 [code] => HL [group] => 0 )
[5] => Array ( [percent] => 88.461538461538 [basenum] => 907 [code] => TL [group] => 0 ) // This will not removed because of 'code' different in array
[6] => Array ( [percent] => 88.461538461538 [basenum] => 907 [code] => HL [group] => 1 ) // This will not removed because of 'group' different in array
))
由于元素0中包含basenum
,因此将删除元素1,2,3,4,5。因此,预期结果/新数组应为
Array (
[0] => Array ( [percent] => 88.461538461538 [basenum] => 079 [code] => HL [group] => 0 )
[1] => Array ( [percent] => 88.461538461538 [basenum] => 907 [code] => TL [group] => 0 )
[2] => Array ( [percent] => 88.461538461538 [basenum] => 907 [code] => HL [group] => 1 )
)
感谢所有答案。
答案 0 :(得分:1)
由于basenum
值相等,如果它们包含相同的字符集,则此代码将执行您想要的操作。 compare_basenum
通过对两个basenum
中的字符进行排序并比较所得数组来检查两个code
值是否相同。在循环中,我们查看所有具有匹配基数的值,并查看group
或function compare_basenum($b1, $b2) {
$b1 = str_split($b1);
sort($b1);
$b2 = str_split($b2);
sort($b2);
return $b1 === $b2;
}
$newdata = array();
foreach ($data as $d) {
$matching_basenums = array_filter($newdata, function ($v) use ($d) { return compare_basenum($v['basenum'], $d['basenum']); });
if (!count($matching_basenums) ||
!in_array($d['code'], array_column($matching_basenums, 'code')) ||
!in_array($d['group'], array_column($matching_basenums, 'group'))) {
$newdata[] = $d;
}
}
print_r($newdata);
值是否相同,如果不同,则将该值添加到输出中。
Array (
[0] => Array ( [percent] => 88.461538461538 [basenum] => 079 [code] => HL [group] => 8 )
[1] => Array ( [percent] => 88.461538461538 [basenum] => 709 [code] => HL [group] => 0 )
[2] => Array ( [percent] => 88.461538461538 [basenum] => 907 [code] => TL [group] => 0 )
[3] => Array ( [percent] => 88.461538461538 [basenum] => 907 [code] => HL [group] => 1 )
)
输出(用于基于以下OP注释的更新数据)
class EODSales
{
public string Sales { get; set; }
public int TC { get; set; }
public decimal Amount { get; set; }
}
class EODBO
{
public string Sales { get; set; }
public List<EODSales> Values { get; set; }
}
.
.
.
.
List<EODSales> EODSalesList = new List<EODSales>();
List<EODBO> eodboList = new List<EODBO>();
.
.
.
//Inside a method -
EODSales sales = new EODSales();
sales.Sales = "Prev";
sales.TC = 5;
sales.Amount = 500;
EODSalesList.Add(sales);
List<EODSales> localEODSalesList = new List<EODSales>();
localEODSalesList.Add(sales);
EODBO obj = new EODBO();
obj.Sales = "Baking Order";
obj.Values = localEODSalesList;
eodboList.Add(obj);
//Inside another method
EODSales sales = new EODSales();
sales.Sales = "Prev";
sales.TC = 10;
sales.Amount = 1000;
EODSalesList.Add(sales);
List<EODSales> localEODSalesList = new List<EODSales>();
localEODSalesList.Add(sales);
EODBO obj = new EODBO();
obj.Sales = "Baking Order";
obj.Values = localEODSalesList;
eodboList.Add(obj);