0

I am trying to recreate a csv file based on another csv source file. I am trying to skip the whole row when that row has a specific word "WORDO". For now I'm only able to replace the data which has "WORDO" by some empty string (""). However I would like to skip the whole row when a match is found, . For now the $keepColumns array still keeps data[1] ( empty, or not) I would like to skip the whole row when "WORDO" is inside $data[1]

<?php

$finalArray = array();
$newFinalData = "new_csv.csv";

if (($finalData = fopen("mycsv.csv", "r")) !== FALSE)
    {

    $newfile = fopen($newFinalData, 'w');

    while (($data = fgetcsv($finalData, 999999, ",",'"')) !== FALSE)
        {

        //If the 1st column has WORDO as text, ideally skip the whole row
        if( substr( $data[1], 0, 5 ) === "WORDO" ) {
            $data[1] = "";
        }

        $keepColumns = array(

            $data[0],
            $data[1],
            $data[2],
            $data[5]

        );

        fputcsv($newfile, $keepColumns, ";",'"');
        }
    fclose($finalData);
    fclose($newfile);
    }


?>
1
  • 3
    Use the word continue; Commented Jun 30, 2016 at 20:34

1 Answer 1

2

You should use the keyword continue to literally skip to the next iteration:

        if( substr( $data[1], 0, 5 ) === "WORDO" ) {
            $data[1] = "";
            continue;
        }
Sign up to request clarification or add additional context in comments.

3 Comments

Incidentally, $data[1] = ""; becomes irrelevant if you're just going to continue.
And I would probably use if(strpos($data[1], "WORDO") === 0)
He might use it later, you never know.

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.