user3766806
user3766806

Reputation: 13

Delete repetitions in a csv file php

i have a csv file like the below example.

SAT1002569,1,questavienedagestione,3,4,5,6,7,8,9,10,11,12,13,,15
SAT1002566,1,questavienedagestione,3,4,5,6,7,8,9,10,11,12,13,g,15
SAT1002567,1,questavienedagestione,3,4,5,6,7,8,9,10,11,12,13,h,15
SAT1002568,1,questavienedagestione,3,4,5,6,7,8,9,10,11,12,13,i,15
SAT1002569,1,questavienedagestione,3,4,5,6,7,8,9,10,11,12,13,g,15
SAT1002571,1,questavienedagestione,3,4,5,6,7,8,9,10,11,12,13,,15

I have to make a php script that first has to find the rows where the first field is the same (the value of the SAT). If it finds two lines with the same first field must go and check which of the two has the fifteenth field blank and must cancel. It must then leave written one with the fifteenth full field.

<?php
 $RigheFileDefinitivo = explode("\n", file_get_contents("File csv/".$date.".csv")); 

    //mi ciclio il file eccomiquacisono.csv e creo un array con numero riga e contenuto riga
    foreach($RigheFileDefinitivo as $num=>$riga)
    {
        //spezzo ogni riga del file in base ad un delimitatore ovvero la ","
        $campi = explode(",", $riga);

        //dico che il contenuto numero 14 dell'array (ovvero della riga) è la data
        $NrSat = $campi[0];
        $Delimitatore = $campi[15];
        foreach($RigheFileDefinitivo as $num=>$riga)
        {
                $campi = explode(",", $riga);
                $NrSat2 = $campi[0];
                $Delimitatore = $campi[15];
                if($NrSat == $NrSat2 && $Delimitatore == ""  )
                {
                    unset($RigheFileDefinitivo[$num]);

                }
        }
    }
    file_put_contents("File csv/".$date.".csv", join("\n",$RigheFileDefinitivo));
    fclose($handle);

?>

I tried to create the script but does not work. I hope I have explained my problem well. Help me Below I will post the script that I wrote

Upvotes: 0

Views: 45

Answers (1)

FuzzyTree
FuzzyTree

Reputation: 32392

Use freadcsv and fputcsv. See comments in code

if (($handle = fopen("test.csv", "r")) !== FALSE) {

    $rows = array(); //indexed by SAT value

    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {                        
        if(isset($rows[$data[0]])) { 
            //if we've seen the SAT value before, 
            //keep the row with the non-empty 16th column
            if(empty($rows[$data[0]][15]) && !empty($data[15])) {
                $rows[$data[0]] = $data;
            }
        }
        else {
            //if this is the 1st time we've seen the SAT value, 
            //add it to the row as is
            $rows[$data[0]] = $data;
        }            
    }

    fclose($handle);

    //write rows to csv
    $fp = fopen('new.csv', 'w');

    foreach ($rows as $row) {
        //there still may be rows with an empty 16th column
        //for example if the csv contains a single SAT value with an empty 16th column
        //if you want to exclude these use an if statement here
        //if(!empty($row[15]))
        fputcsv($fp, $row);
    }

    fclose($fp);    
}

Upvotes: 1

Related Questions