SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Delete Repeated Data in CSV File

    Hiya,

    I have a CSV file, that contains people's Ages, First names, and Surnames. If an age is duplicated more than 7 times, how do I delete the Firstname and Surname from the file for that age?

    Example:

    15, Danial, Green
    22, Neil, Hall
    15, Phil, Sheen
    17, Micheal, Snowfield
    15, Peter, Davis
    15, Simon, Scotland
    77, Dave, Erin

    *The 6th row (Shown in Red) should be deleted.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why delete 15, Simon, Scotland? Because you want to keep the first 7 occurances of the age 15, and discard all others?

  3. #3
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Of course, thanks for noticing that. I was supposed to copy and paste the data a few times

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $fp_source fopen('old.csv''rb');
    $fp_new fopen('new.csv''wb');
    $age_count = array();
    while (
    false !== $cols fgetcsv($fp_source1000)) {
        
    $age $cols[0];
        isset(
    $age_count[$age]) or $age_count[$age] = 0;
        if (++
    $age_count[$age] <= 7) {
            
    fputcsv($fp_new$cols);
        }


  5. #5
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The above code doesn't work properly. If I enter less than 7 duplicates, the output file doesn't contain anything?


Bookmarks

Posting Permissions

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