Question

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
        )
)
Was it helpful?

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);
}

OTHER TIPS

Note:

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.

    if ($c == 1 && current($csvData) === null) {
        break;
    }

You can run a extra loop for checking empty

while(! feof($file)){
        $current_data = fgetcsv($file);   


        for ($i=0; $i < $total_title ; $i++) {    
            if( empty( $current_data[ $i ] )  ){                    
                unset($information[$itt]);
                break;
            }
            $information[$itt][ $titles[$i] ] = $current_data[$i] ;                
        }            
        $itt = $itt+1;            
    }  

I recommend this php function that does exactly what you want

http://www.php.net/manual/es/function.file.php

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top