使用array_search函数时数组中的第一个值重复

时间:2019-04-04 10:26:57

标签: php arrays multidimensional-array array-column

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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 —将用户提供的函数应用于数组的每个成员

Demo