Search code examples
phpcsvfgetcsv

PHP CSV File Stop on Blank Lines


I'm trying to process an uploaded CSV file into an array in PHP. I have that working fine, but some files from users end up have a bunch of blank rows with delimiters. This usually happens when they use Excel on an existing file. Highlighting the old cells and just clearing them.

Sample CSV File

lineNo,date,vendor,amount
1,5/2/2012,V000236,3727.21
2,5/2/2012,V003432,4826.19
,,,
,,,

Becomes the following array

Array
(
    [0] => Array
        (
            [0] => lineNo
            [1] => date
            [2] => vendor
            [3] => amount
        )

    [1] => Array
        (
            [0] => 1
            [1] => 5/2/2012
            [2] => V000236
            [3] => 3727.21
        )

    [2] => Array
        (
            [0] => 2
            [1] => 5/2/2012
            [2] => V003432
            [3] => 4826.19
        )

    [3] => Array
        (
            [0] => 
            [1] => 
            [2] => 
            [3] => 
        )

    [4] => Array
        (
            [0] => 
            [1] => 
            [2] => 
            [3] => 
        )
)

I don't want to just remove any blank rows, I want to stop after array index 2. Go easy on my function I'm new :P

function csvToArray($csvFile, $specialChars = FALSE) {
$arrayCSV = array();

if (($csvHandle = fopen($csvFile, "r")) !== FALSE) { // Open the CSV
    $csvKey = 0; // Set the parent array key to 0

    while (($csvData = fgetcsv($csvHandle)) !== FALSE) {
        $c = count($csvData); // Count the total keys in each row

        //need something to stop on blank delimiter rows ,,,

        for ($x = 0; $x < $c; $x++) { //Populate the array
            if ($specialChars === TRUE) {
                $arrayCSV[$csvKey][$x] = htmlspecialchars($csvData[$x]);
            } else {
                $arrayCSV[$csvKey][$x] = $csvData[$x];
            }
        }
        $csvKey++;
    }

    fclose($csvHandle);
}

return $arrayCSV;
}

Ultimately, I'd like this returned

Array
(
    [0] => Array
        (
            [0] => lineNo
            [1] => date
            [2] => vendor
            [3] => amount
        )

    [1] => Array
        (
            [0] => 1
            [1] => 5/2/2012
            [2] => V000236
            [3] => 3727.21
        )

    [2] => Array
        (
            [0] => 2
            [1] => 5/2/2012
            [2] => V003432
            [3] => 4826.19
        )
)

Solution

  • Can't you break; your while loop as soon as you find an empty value?

    if (($csvHandle = fopen($csvFile, "r")) !== FALSE) { // Open the CSV
        $csvKey = 0; // Set the parent array key to 0
    
        while (($csvData = fgetcsv($csvHandle)) !== FALSE) {
            $c = count($csvData); // Count the total keys in each row
    
            // ## Flag variable ##########
            $empty = true;
    
            for ($x = 0; $x < $c; $x++) { //Populate the array
    
                // ## Test each value ##########
                $empty = $empty && (empty($csvData[$x]));
    
                if ($specialChars === TRUE) {
                    $arrayCSV[$csvKey][$x] = htmlspecialchars($csvData[$x]);
                } else {
                    $arrayCSV[$csvKey][$x] = $csvData[$x];
                }
            }
    
            // ## Stop loop if all empty ##########
            if ($empty) {
                 unset($arrayCSV[$csvKey]);
                 break;
            }
            $csvKey++;
    
        }
    
        fclose($csvHandle);
    }