Jump to content

Merge duplicate rows into one CSV


devofash

Recommended Posts

Hi Guys,

 

Need a bit of help.

 

Basically I want to merge duplicate records into one in an excel csv file.

 

example:

col1    -  col2   - col3 - col4
user 1 -  A
user 2 -            B
user 3 -                       C
user 1 -             B
user 1 -                       C

into

col1    -  col2   - col3 - col4
user 1 -  A         B       C 
user 2 -             B
user 3 -                      C

 

 

I have a form where user submits the information and it is then stored in the csv file. I have managed to store the data into the csv but can't seem to sort the data as required. Was wondering if anyone can help me out with a bit of code. I dont know where to start with merging so I don't have any code to post.

 

Any help is much appreciated.

Thanks dev

 

Link to comment
Share on other sites

Thanks for coming back to me.

 

forgot to mention I need to keep the orignal csv, what I need is a script that will read all the records from the orignal file and sort it out according to the requirement and save a new copy.

 

Its just a html form with 2 input fields called account number and order number.

 

This is the code I use to write to the CSV file.

$cvsData  = $accountnumber . $ordernumber . "\n";
        $fp       = fopen("datafile/registrations.csv", "a"); 
        
        if($fp)
        {
            fwrite($fp, $cvsData);    
        	fclose($fp);  
        } 
        else 
        {

        }

 

Link to comment
Share on other sites

Your csvData variable is assigned the value of account number and order number, but it appears that there is no usage of the comma to separate the values.

 

It should be something like this

 

$cvsData  = $accountnumber . "," .$ordernumber . "\n";

 

What you should do is before saving to the csv file. Store the data into a 2D array as such.

 

$DataColumn[] = $accountnumber;

$DataColumn[] = $ordernumber;

 

$DataRow[] = $DataColumn;

 

Now that you have the data stored into a 2D array you can do some sorting using some of the array functions.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.