ylzyh
ylzyh

Reputation: 13

PHP fgetcsv generated associative array key error

I just wrote a php function which will read data from a csv file and store the data into an array with each row data as an element. Each element is an associative array keyed by csv headers.

The function looks like this:

function parse_csv($csv_path) {

    if(is_file($csv_path)&&file_exists($csv_path)) {
        $csv_data = array();
        $csv_header = array();
        $n = 0;
        try {
            $csv_file = fopen($csv_path, "r");
            while (!feof($csv_file)) {
                $csv_line = fgetcsv($csv_file); 
                if(array(NULL) !== $csv_line && !empty($csv_line)) {
                    $n++;
                    if($n == 1) {
                        $csv_header = $csv_line;
                    }
                    else {
                        $csv_row = array();
                        for($i=0;$i<count($csv_header);$i++) {
                            $csv_row[$csv_header[$i]] = $csv_line[$i];
                        }
                        $csv_data[] = $csv_row;
                    }
                }
            }

            return $csv_data;
        }
        catch (Exception $e) {
            return FALSE;
        }
    }
    else {
        return FALSE;
    }
}

I've done a test and used print_r to print the array ruturned by the function, like the following:

$csv = parse_csv('D:\countries.csv');
print_r($csv);

The output is:

Array
(
    [0] => Array
        (
            [TermName] => Afghanistan
            [TermDescription] => 
            [TermFormat] => 
        )

    [1] => Array
        (
            [TermName] => Albania
            [TermDescription] => 
            [TermFormat] => 
        )

    [2] => Array
        (
            [TermName] => Algeria
            [TermDescription] => 
            [TermFormat] => 
        )

    [3] => Array
        (
            [TermName] => American Samoa
            [TermDescription] => 
            [TermFormat] => 
        )

    [4] => Array
        (
            [TermName] => Andorra
            [TermDescription] => 
            [TermFormat] => 
        )
)

But when I tried to access the individual element like this

print($csv[1]["TermName"]);

I got the error: Undefined index: TermName in C:\xampp\htdocs\test\test.php on line 49.

This really confuses me. Am I missing something?

Found the reason:

Because I save the csv file with Windows notepad, it added BOM to the file. So the first header becomes to [BOM]TermName. After remove the BOM, it's all good now.

Upvotes: 1

Views: 655

Answers (1)

ins0
ins0

Reputation: 3928

you can short your code and make it more readable to find errors faster like this.

function parse_csv($csv_path) {

    if(is_file($csv_path) && file_exists($csv_path)) {

        $stream = fopen($csv_path, 'r');
        $csvHeader = fgetcsv($stream);
        $csvData = [];
        while(!feof($stream))
        {
            $lineData = [];
            $line = fgetcsv($stream);

            if( count($csvHeader) != count($line) )
            {
                throw new \Exception('Line '.count($csvData)+1.' got more data then headers!');
            }

            foreach($csvHeader as $x => $header)
            {
                $lineData[$header] = $line[$x];
            }

            $csvData[] = $lineData;
        }
        return $csvData;
    }
    return false;
}

Upvotes: 0

Related Questions