0

I have a string of user data comma separated as below and pipe separated.I have been trying to remove duplicates by converting it to array. And even with php But it all does not works. My data looks like below:

Marja_Roxburgh|abc@abc.com|123-456-7890|N/A|2011-11-17|N/A|N/A|N/A|N/A|N/A|120,
Santa_Roxburgh|bmw@abc.com|123-456-7890|N/A|2013-11-17|N/A|N/A|N/A|N/A|N/A|10,
Marja_Roxburgh|abc@abc.com|123-456-7890|N/A|201-11-17|N/A|N/A|N/A|N/A|N/A|300,
Saga_Shera|xyz@abc.com|123-456-7890|N/A|2013-11-17|N/A|N/A|N/A|N/A|N/A|0,
Marja_Roxburgh|abc@abc.com|123-456-7890|N/A|2013-11-17|N/A|N/A|N/A|N/A|N/A|120

I have tried What's the best way to remove duplicates from a string in PHP (or any language)?, detecting duplicate string in a explode function php and like this How to detect duplicate values in PHP array? one too. But none of these works in my case. Coz my data is not unique for same User say Marja_Roxburgh. I wants to remove all entries of Marja_Roxburgh based on its email but also wants to keep the first one only and sum all amounts from its transactions. I've been googling for any logic Im not able to understand what to do this it. How Do I keep only first Record of Marja_Roxburgh and remove all other of her. And also sum all her Ammounts before removing her data?

I'm missing the logic to understand and solve this problem. Can somebody help me understand this logic? any Ideas?

Community
  • 1
  • 1
Jaguar
  • 403
  • 2
  • 10
  • 22

1 Answers1

-1

I hope this may help u (sorry for dirty code :) )

// supperate it
$arr = explode( "," , $s );

//print_r($arr);

// you can skip this
$re = array_unique($arr);

//print_r($re);

$another = array();

foreach($re as $each){
    //print_r($each);
    $tmp = explode( "|" , $each );

    // you add your other conditions here
    if( !in_array('abc@abc.com',$tmp) ){
        $another[] = $tmp;
    }   
}

print_r($another);

$another1 = array();

// Back to input format

foreach($another as $a1){
    $another1[] = implode('|',$a1);
}

print_r($another1);


echo $result = implode(',',$another1);

// output
Santa_Roxburgh|bmw@abc.com|123-456-7890|N/A|2013-11-17|N/A|N/A|N/A|N/A|N/A|10,
Saga_Shera|xyz@abc.com|123-456-7890|N/A|2013-11-17|N/A|N/A|N/A|N/A|N/A|0