将空CSV值替换为-PHP

将空CSV值替换为-PHP,php,csv,opencart,Php,Csv,Opencart,Im为OpenCart使用CSV导入插件,默认情况下,如果产品缺少任何列字段,则完全跳过这些产品。 无论如何,我都可以读取文件并将所有空字段替换为“null”或0,然后将其发送回代码 跳过下面的检查会导致读取/放置格式出现偏移 $fh = fopen($file, 'r'); if(!$fh) die('File no good!'); // Get headings $headings = fgetcsv($fh, 0, $delim); $num_c

Im为OpenCart使用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];
        }
$fh=fopen($file,'r');
如果(!$fh)死亡('File no good!');
//获取标题
$headers=fgetcsv($fh,0,$delim);
$num_cols=计数($headers);
$num_行=0;
//以csv格式读取文件
while(($row=fgetcsv($fh,0,$delim))!==FALSE){
//如果此行中的num列与num标题不同,则缺少产品
如果(计数($row)!=num\u cols){
$this->total_items_missed++;
继续;
}   
对于($i=0;$i尝试


对于($i=0;$i替换这两行

$this->total_items_missed++;
continue;


这将使用0添加任何缺少的值

删除执行检查的4行,具体取决于您对下一个数据所做的操作,这可能就是您所需要的。这只会导致此处出现偏移,$raw_prod[$headers[$i]]=$row[$i];谢谢,效果很好!我不确定下面的一个是否更合适,我想它们会提供相同的结果。我自己从来没有使用过array_pad B。只有在删除了列计数检查后,才能使用它……所以不,它不会像你想的那样工作。array pad是你必须记住的功能之一。我自己几乎从未使用过它任何一个
$this->total_items_missed++;
continue;
$row = array_pad($row, $num_cols, 0);