1

I am trying to remove duplicates from two dimensional array based on client ID. The script removes all duplicates EXCEPT the first value, first client.

I have tried few more ways of comparing result from array_search to different values, trying to use !== and === with no promising result.

Inserting non numeric value as first in the array, makes everything deduplicate flawlessly.

Here is the code:

// 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;
    }
}

The final result is a client and a sum up value from two dimensional array. Everything works as it should except for the first client, that appears multiple times in the new build client list without duplicates.

Here's the Input Array

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 
    )
)

And desired output - customer id 50245901 is not appearing multiple times:

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 
    )
)
Rahul
  • 18,271
  • 7
  • 41
  • 60
Wesan
  • 57
  • 2
  • 7
  • 1
    Can you show what your input array looks like, and what the expected result of that should be? – Qirel Apr 04 '19 at 10:27
  • Please post input array values as well as what expected outcome you want finally – Alive to die - Anant Apr 04 '19 at 10:29
  • Hello. I have added the array values example and the outcome to the question. – Wesan Apr 04 '19 at 10:52
  • I see two interesting but different ways to resolve it, but does anyone additionaly know why does this happen, that the value for the first array keeps adding itself? Is it because False == 0 and the place in array for it is 0? – Wesan Apr 04 '19 at 12:48

2 Answers2

1

You need to simplify your foreach() like below:-

$ClientList = array();

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

Output:-https://3v4l.org/f7Bfn

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • Thank you kindly. This way works perfectly. Though I am still confused about the issue that was caused by array search in the original code. – Wesan Apr 05 '19 at 07:57
1

You can simply write code,

$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 — Return all the values of an array
array_unique — Removes duplicate values from an array
array_column — Return the values from a single column in the input array
array_walk — Apply a user supplied function to every member of an array

Demo.

Rahul
  • 18,271
  • 7
  • 41
  • 60