15

I have this script that I did, it basically grabs all the files in my "logs" folder and merge them all in one array file, my only problem is that, sometimes the script breaks if there is blank line or empty line! how can I tell it to automatically skip blank empty lines and go to next? blank lines are not necessarily at the top or bottom! could be in the middle of the csv file

<?php
    $csv = array();
    $files = glob('../logs/*.*');
    $out = fopen("newfile.txt", "w");

    foreach($files as $file){
        $in = fopen($file, "r");

    while (($result = fgetcsv($in)) !== false)

        {   
            $csv[] = $result;
        }

        fclose($in);
        fclose($out);
    }

    print json_encode(array('aaData' => $csv ));
?>
hyperexpert
  • 532
  • 1
  • 8
  • 20
  • Maybe `if (is_array($results) && count($result)) { $csv[] = $result; }` – Orangepill Aug 19 '13 at 22:53
  • 3
    @Orangepill: `fgetcsv()` returns `array(null)` when the line is blank, so unfortunately, `is_array($result) && count($result)` would be `true`. `if ($result !== array(null)) { $csv[] = $result; }` should do the trick. – Tomas Creemers Aug 19 '13 at 22:59
  • @TomasCreemers That worked great :D would you please post it as an answer – hyperexpert Aug 19 '13 at 23:03

3 Answers3

23

As you can read in the documentation for fgetcsv():

A blank line in a CSV file will be returned as an array comprising a single null field, and will not be treated as an error.

Checking for that before adding it to your data array should be sufficient:

while (($result = fgetcsv($in)) !== false) {
    if (array(null) !== $result) { // ignore blank lines
        $csv[] = $result;
    }
}
Tomas Creemers
  • 2,645
  • 14
  • 15
  • 2
    this solution would not catch the case of an 'empty' csv line containing no values, like this ',,,,,,,,' which would be translated as an array with empty string values – Pitt Jan 18 '17 at 19:52
  • 2
    @Pitt You are right. I was about to use this solution but it didn't work when I tested it. In that case, you can use the accepted answer from this question: https://stackoverflow.com/questions/5040811/checking-if-all-the-array-items-are-empty-php – Cave Johnson Jun 01 '17 at 19:39
5

This works 100% tested, simplest way. The explanation is that blank lines make fgetcsv return a non-empty array with just a null element inside.

if ($result[0] == NULL)
    continue;
rubydio
  • 105
  • 2
  • 7
  • 1
    This is correct in some cases, in some it will skip valid lines. For instance if the first column is numeric and zero (0) then your condition will be met and the line would be wrongly skipped. The better way to go is replace the condition with ```if (count($result) == 1 && $result[0] === null)```, which make sure there's only one value in the array and the value is exactly null, not nullish like 0 or empty string. – The Coprolal Mar 09 '21 at 10:35
2

In short

$csv = array_map('str_getcsv', file($file_path, FILE_SKIP_EMPTY_LINES|FILE_IGNORE_NEW_LINES));

Explanation

  1. file reads the content of the file into an array. The FILE_SKIP_EMPTY_LINES will skip the empty lines in the file.
  2. array_map will apply the function str_getcsv on each element of the array. str_getcsv will parse the string input for fields in csv format and return an array containing the fields.

Read more about str_getcsv

Read more about file

Read more about array_map

ash__939
  • 1,614
  • 2
  • 12
  • 21
  • Not good: this will bug out on multi-line csv data (when the data contains line breaks) due to the file() function. The accepted solution is the one to use. – Steve Horvath Sep 15 '21 at 23:23
  • Did you check this? If PHP is not properly recognizing the line endings when reading files either on or created by a Macintosh computer, enabling the auto_detect_line_endings run-time configuration option may help resolve the problem. – ash__939 Sep 16 '21 at 06:25
  • I suggest you test it. The file() function is the wrong tool for handling multi-line csv values. – Steve Horvath Sep 19 '21 at 22:44
  • @SteveHorvath You're right. Thanks for pointing out the issue. – ash__939 Sep 21 '21 at 08:10