0

I need to replace the values only from the third column in my csv. And no where else. Column 3 of my csv contains only a 'J' or a'N' and i need to change them to '5' or '0'. But right now i it changes always all found N's and J#s in the whole file. But i need it only updated in the third column.

I use the following code to open and rewrite my csv. But it writes always all found Letters to 5 or 0.

$csvfile = 'csvfile.csv';
if (file_exists($csvfile)) {
        unlink($csvfile);
    echo "Alte $csvfile entfernt!";
} else {
    echo "Keine alte $csvfile vorhanden";
}

$row = 0;
$fp = fopen('csvfile.csv', 'w');
if (($handle = fopen("oldCSV.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 200000, ",")) !== FALSE) {
    $num = count($data);
    $dataold = ['J', 'N'];                                                            
    $datanew = ['5', '0'];
    echo "<p> $num Felder in Zeile $row: <br /></p>\n";
    $row++;
    for ($c=0; $c < $num; $c++) {
        fputs($fp, str_replace($dataold, $datanew, $data[$c] . "\n"));
    }
  }
}

fclose($fp);
2
  • 2
    remove for loop, str_replace only $data[2] Commented Apr 28, 2016 at 17:17
  • Indeed, don't for, just do $data[2] the whole time. Commented Apr 28, 2016 at 17:21

3 Answers 3

1
while ($data = fgetcsv($handle)) {

    $data[2] = str_replace(['J', 'N'], ['5', '0'], $data[2]);

    fputcsv($fp, $data);

    echo "<p> ".count($data)." Felder in Zeile ".$row++.": <br /></p>\n";
}

EDIT

  1. I removed !== FALSE part from while to not compare result with false each time, finally fgetcsv() will return false itself and stop the loop
  2. collapsed str_replace and its' arguments-arrays (for readability only)
  3. removed for loop to change only 3rd ([2]) coloumn
  4. got rid of $num variable
Sign up to request clarification or add additional context in comments.

4 Comments

Although this code may answer the question, providing additional context regarding why and/or how it answers the question would significantly improve its long-term value. Please edit your answer to add some explanation.
no. doesn't work. at least not for me. it throws also a ',' in all empty fields in the 4. column but doesn't change the Letters to the desired numbers in column 3. unfortunately also the other suggestions seems not to work for me.
I have to correct me. This solution works as it should be. my mistake was that i saved the 'oldSCV' with a';' as separator. so i had to change it in my code too. while (($data = fgetcsv($handle, 200000, ";")) so thanks all your help. :)
yep, I removed 2nd and 3rd parameters in my answer, cause they're default therefore excessive in your example, but if you have ; separator don't abandon them
1

Lot's of ways and you might get lots of answers. Here's one:

switch($data[2]) {
    case 'J':
        $data[2] = '5';
        break;
    case 'N':
        $data[2] = '0';
        break;
}
fputcsv($fp, $data);

Comments

0

You're looping through each column of your CSV. Why not just deal with the specific column? A simple ternary could do this

$data[2] = ($data[2] == 'J') ? '5' : '0'; 

2 Comments

@vladkras According to the OP, the column has only two values
correct. in the third column are all fields filled with N or J.

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.