thephpleague / csv

CSV data manipulation made easy in PHP
https://csv.thephpleague.com
MIT License
3.33k stars 333 forks source link

How do you edit data in a row? #472

Closed jason-engage closed 2 years ago

jason-engage commented 2 years ago

I'm not seeing anything in the documentation about editing/updating row data? Do I have to create a new csv and copy over all the data that I don't want to edit and then insert a new row that contains the edited data?

nyamsprod commented 2 years ago

@jason-engage thanks for using the library. Indeed your are correct since the package is based on stream you can only read from or write to a stream. Updating/Editing is not an option.

Depending on what you want to do with the updated data. Generally speaking, that operation is performant and fast.

jason-engage commented 2 years ago

@jason-engage thanks for using the library. Indeed your are correct since the package is based on stream you can only read from or write to a stream. Updating/Editing is not an option.

Depending on what you want to do with the updated data. Generally speaking, that operation is performant and fast.

You should consider adding an ::Updater to your package, and use a non-stream solution for it. It's a critical feature and feels incomplete without it. Anyways I did end up using it and found a workaround for my case. Cheers

nyamsprod commented 2 years ago

@jason-engage I am always open to PR including that feature to the package 😉

hxdev commented 1 year ago

@jason-engage thanks for using the library. Indeed your are correct since the package is based on stream you can only read from or write to a stream. Updating/Editing is not an option. Depending on what you want to do with the updated data. Generally speaking, that operation is performant and fast.

You should consider adding an ::Updater to your package, and use a non-stream solution for it. It's a critical feature and feels incomplete without it. Anyways I did end up using it and found a workaround for my case. Cheers

@jason-engage Hi, could you please share your workaround?

jason-engage commented 1 year ago

@hxdev

@jason-engage Hi, could you please share your workaround?

Sure, basically, just add the data to the end, and then de-dupe the csv. The CSV unique is custom to my csv type. 2 columns, with column 1 being a unique ID. You'll have to modify to fit your data structure, based on the unique column.

//REMOVE DUPLICATES
      $old = csvParse($csv_path);
      $unique = csvUnique($old);
      csvUpdate($unique, $csv_path);

// Read data from csv and return data
function csvParse(string $csv = null): array {
    $data = [];
    $data = array_map('str_getcsv', file($csv));

    return $data;
}

// Remove duplicate values from data
function csvUnique(array $data = []): array {
    $result = [];

    foreach ($data as $key => $val) {
        if ( !empty($val[0]) && !empty($val[1]) ) {
           $result[$val[0]] = $val[1];
       }
    }

    $data = [];

    foreach ($result as $key => $val) {
        $data[] = [$key,$val];
    }

    return $data;
}

// Update csv with unique data
function csvUpdate(array $unique = [], string $csv = null): void {
    $fp = fopen($csv, 'w');

    foreach($unique as $fields) {
        fputcsv($fp, $fields);
    }

    fclose($fp);
    echo "Done!".PHP_EOL;
}