【问题标题】:PHP !empty() working functionality issue?PHP !empty() 工作功能问题?
【发布时间】:2022-01-25 08:35:14
【问题描述】:

我有一个包含此类行的 csv 文件

这个文件有 240 行上面的数据,不知何故它有 700+ 空行.. 当我使用此代码检查行时

function fn_get_order_csv($file){

    $all_rows = array();
    
    if (($handle = fopen($file[0]['path'], "r")) !== FALSE) {
        
        $headers = fgetcsv($handle);
        
        if(!empty($headers)){
            foreach($headers as $key=>$value){
                $headers[$key] = str_replace( ':','_',preg_replace('!\s+!', '_',  strtolower( trim($value))) );
            }
        }
        while ($row = fgetcsv($handle)) {
            if(!empty($row)){
                $all_rows[] = array_combine($headers, $row);
            }
        }
    }
    
    return $all_rows;
}

在上面的代码中,If(!empty($row)) 仍然计算空行并返回所有 900+ 行 但是,如果我将其更改为 If(!empty($row[4])) 它只返回 240 行.... 我缺少什么来正确理解它?

【问题讨论】:

标签: php


【解决方案1】:

empty 函数 documentation 声明:

如果变量不存在或者其值为 false,则认为该变量为空。

fgetcsv 函数documentation 声明:

CSV 文件中的空行将作为包含单个空字段的数组返回,不会被视为错误。

包含单个空字段的数组既不是假的,也不是未设置的。尝试改用if($row === [null])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多