Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do you edit data in a row? #472

Closed
jason-engage opened this issue Sep 2, 2022 · 5 comments
Closed

How do you edit data in a row? #472

jason-engage opened this issue Sep 2, 2022 · 5 comments

Comments

@jason-engage
Copy link

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
Copy link
Member

@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
Copy link
Author

jason-engage commented Sep 4, 2022

@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
Copy link
Member

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

@hxdev
Copy link

hxdev commented Oct 24, 2022

@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
Copy link
Author

jason-engage commented Oct 24, 2022

@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;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants