用某些内容替换空的 CSV 值 - PHP

发布于 2024-12-11 19:30:28 字数 719 浏览 3 评论 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];
        }

Im using a CSV Import plugin for open cart and by default is completely skips products if they are missing any of the column fields.
Is there anyway i can read the file and replace all the empty fields with 'null' or 0 and send it back to the code.

skipping that check below causes an offset in the reading/placing format !

    $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];
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡淡離愁欲言轉身 2024-12-18 19:30:28

将这两行替换

$this->total_items_missed++;
continue;

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

Which 将会用 0 添加所有缺失值

Replace these two lines

$this->total_items_missed++;
continue;

with

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

Which will add any missing values with 0's

热风软妹 2024-12-18 19:30:28

尝试

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

try

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
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文