Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Remove all instances of a string but keep only one in php also sum amounts

    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:

    Code:
    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 Amounts before removing her data?

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

    thanks

  • #2
    Regular Coder
    Join Date
    Jun 2009
    Posts
    144
    Thanks
    3
    Thanked 20 Times in 20 Posts
    Are you placing them in a single array? What should the duplicate search by as none of those are identical..

  • #3
    New Coder
    Join Date
    May 2005
    Location
    New Zealand
    Posts
    76
    Thanks
    0
    Thanked 6 Times in 6 Posts
    If the email address is the thing to determine uniqueness then you can build an assoative array keyed by email address. In the loop to build this array you check if an item with the email address exists, if not then add it, if so then just increment the amount.

    PHP Code:

    $exploded_data 
    = array(/* etc as you have exploded on comma then bar (|) */);
    $final_data = array();

    foreach(
    $exploded_data as $row)
    {
       
    // Note you probably need to replace $row['email_address'] with the index the email address is at, which would by 1 looking at your source data (i.e. $row[1]). 
       
    if (isset($final_data[$row['email_address']))
        {
             
    // If record with that email address exists then add to amounts.
             
    $final_data[$row['email_address']['amount'] += $row['amount'];
        }
        else
        {
            
    // This is the first time we have encountered row with the email address so add whole row to the final_data array. Key by email address.
            
    $final_data[$row['email_address'] = $row;
        }
    }

    // Then use final data as wanted. 
    Last edited by DougMck; 11-18-2013 at 03:58 AM.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •