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

これらの2行を置き換えます

$this->total_items_missed++;
continue;

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

これにより、欠落している値が0で追加されます

于 2011-10-24T20:36:45.033 に答える