我正在尝试根据客户端 ID 从二维数组中删除重复项。该脚本会删除除第一个值、第一个客户端之外的所有重复项。
我尝试了几种将 array_search 的结果与不同值进行比较的方法,尝试使用 !== 和 === 却没有得到有希望的结果。
将非数值作为数组中的第一个插入,可以完美地消除重复数据。
这是代码:
// Build client list
$ClientList = array();
$counter = 0;
foreach ($ClientTrans as $order => $value) {
$ClientId = $ClientTrans[$order]['customer_id'];
if (array_search($ClientId, array_column($ClientList, 0)) == FALSE && is_numeric($ClientId)) {
$ClientList[$counter][] = $ClientId;
$counter += 1;
}
}
最终结果是一个客户端和一个二维数组的总和值。除了第一个客户端之外,一切都正常工作,它在新的构建客户端列表中多次出现,没有重复。
这是输入数组
Array (
[0] => Array (
[customer_id] => 50245901
[points] => 299
)
[1] => Array (
[customer_id] => 50245907
[points] => 3847
)
[2] => Array (
[customer_id] => 50245908
[points] => 159
)
[3] => Array (
[customer_id] => 50245910
[points] => 3175
)
[4] => Array (
[customer_id] => 50245914
[points] => 641
)
[5] => Array (
[customer_id] => 50245916
[points] => 449
)
[6] => Array (
[customer_id] => 50245921
[points] => 551
)
[7] => Array (
[customer_id] => 50245927
[points] => 0
)
[8] => Array (
[customer_id] => 50245928
[points] => 602
)
[9] => Array (
[customer_id] => 50245929
[points] => 495
)
[10] => Array (
[customer_id] => 50245931
[points] => 539
)
[11] => Array (
[customer_id] => 50245941
[points] => 0
)
[12] => Array (
[customer_id] => 50245901
[points] => 124
)
[13] => Array (
[customer_id] => 50245901
[points] => 512
)
)
和所需的输出 - 客户 ID 50245901 没有多次出现:
Array (
[0] => Array (
[customer_id] => 50245901
)
[1] => Array (
[customer_id] => 50245907
)
[2] => Array (
[customer_id] => 50245908
)
[3] => Array (
[customer_id] => 50245910
)
[4] => Array (
[customer_id] => 50245914
)
[5] => Array (
[customer_id] => 50245916
)
[6] => Array (
[customer_id] => 50245921
)
[7] => Array (
[customer_id] => 50245927
)
[8] => Array (
[customer_id] => 50245928
)
[9] => Array (
[customer_id] => 50245929
)
[10] => Array (
[customer_id] => 50245931
)
)