0

I have a .CSV file with static column names. I receive it daily so i have to automatically edit it on a daily base.

On the first line are the row names for example: row1;row2;row3,row4,row5

for example when i want to unset "row2" and "row4".

How can i unset multiple rows based on a name?

I found a some tutorials about deleting lines or rows based on a row position but nothing that helps me completely.

This is what is have now:

$inFile  = 'original.csv';
$outFile = 'edited.csv';

$delimiter = ';';
$enclosure = '"';   

$read = fopen($inFile, 'r');
$write = fopen($outFile, 'w');
if ($write && $read) {
    while (($data = fgetcsv($read)) !== FALSE) {

        // how to unset multiple row names

        fputcsv($write, $data, $delimiter, $enclosure);
    }
}
fclose($write);
fclose($read);

Also, do i need to use the delimiter and enclosure when i fopen the original file?

4
  • 1
    you don't delete rows from a csv. you just don't output them into a new csv file. if (row doesn't contain what I"m deleting) { output row } Commented Oct 23, 2015 at 21:38
  • Yes thank you, i changed everything to "unset" instead of "delete" Commented Oct 23, 2015 at 21:45
  • CSV is not a database format, and you cannot work with it the way you would a database. You will need to write code manually to manipulate the data in string values. Commented Oct 23, 2015 at 21:57
  • More specifically, you'd need to make use of file_get_contents(), file_put_contents() ,explode(), implode(), and if/then logic, not to mention loops. Commented Oct 23, 2015 at 21:58

1 Answer 1

1

Hi you can try the updated following code:

$inFile  = 'original.csv';
$outFile = 'edited.csv';
$delimiter = ';';
$enclosure = '"';   
$removeFields = array('color');

$write = fopen($outFile, 'w');

if ($write) {
    $rows = file($inFile);
    $first = false;
    foreach($rows as $row) {
        $csvToPHP = str_getcsv($row, $delimiter, $enclosure);
        if (!$first) {
            $headers = array_flip($csvToPHP);
            $first = true;
        }

        foreach($removeFields as $remove) {
            unset($csvToPHP[$headers[$remove]]);
        }
        fputcsv($write, $csvToPHP, $delimiter, $enclosure);
    }   
}
fclose($write);

I used a test csv original.csv:

name,age,color
test1,20,red
test2,32,blue
test3,92,green
test4,12,red
test5,56,orange

Result edited.csv:

name
test1
test2
test3
test4
test5

Hope it helps. Good luck!

Sign up to request clarification or add additional context in comments.

2 Comments

This looks very promising but it doesn't work (yet). Could it have something to do with the delimiter and enclosure when i fopen the original file? They are not the same..
I have updated the answer. It is now using the delimiter also by reading the file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.