0

我使用 CSV 导入插件打开购物车,默认情况下,如果产品缺少任何列字段,则完全跳过产品。无论如何我可以读取文件并用'null'或0替换所有空字段并将其发送回代码。

跳过下面的检查会导致读取/放置格式的偏移!

    $fh = fopen($file, 'r');
    if(!$fh) die('File no good!');

    // Get headings
    $headings = fgetcsv($fh, 0, $delim);
    $num_cols = count($headings);
    $num_rows = 0;

    //Read the file as csv
    while (($row = fgetcsv($fh, 0, $delim)) !== FALSE) {

         //missed product if num columns in this row not the same as num headings
     if (count($row) != $num_cols) {
        $this->total_items_missed++;
        continue;
        }   


        for ($i=0; $i<count($headings); $i++) {
            $raw_prod[$headings[$i]] = $row[$i];
        }
4

2 回答 2

1

尝试

for($i = 0 ; $i<count($headings) ; $i++){
    if(!empty($row[$i])){
        $raw_prod[$headings[$i]] = $row[$i];
    }else{
        $raw_prod[$headings[$i]] = 0;//or what ever value you want
    }
}
于 2011-10-24T20:19:13.787 回答
1

替换这两行

$this->total_items_missed++;
continue;

$row = array_pad($row, $num_cols, 0);

这将添加任何带有 0 的缺失值

于 2011-10-24T20:36:45.033 回答