1

我正在尝试根据客户端 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 
    )
)
4

2 回答 2

1

您需要简化foreach()如下: -

$ClientList = array();

foreach ($ClientTrans as $order => $value) {
    $ClientList[$value['customer_id']]['customer_id'] = $value['customer_id'];
}
$ClientList = array_values($ClientList);

输出:- https://3v4l.org/f7Bfn

于 2019-04-04T11:06:08.867 回答
1

您可以简单地编写代码,

$arr = array_values(array_unique(array_column($arr, 'customer_id')));
$temp = [];
array_walk($arr, function(&$item,$key) use(&$temp){
    $temp[]['customer_id'] = $item; 
});
print_r($temp);

array_values - 返回数组的所有值
array_unique - 从数组中删除重复值
array_column - 从输入数组中的单个列返回值
array_walk - 将用户提供的函数应用于数组的每个成员

演示

于 2019-04-04T11:29:35.140 回答