完整数组计算为空和null


Full array evaluating as empty and null

我有一个很奇怪的问题,我一直找不到答案。我有一个PHP函数,它将CSV数据读取到数组中如果数据读取成功则返回true并通过引用变量

将数组传递回去
function ReadCsvDataIntoArray($path, &$headers, &$array, $idFilter = NULL){     
    if(file_exists($path)){
        $fh = fopen($path, 'r');
        if($fh){
            $headers = fgetcsv($fh);
            $rowIdx = 0;
            while($row = fgetcsv($fh)){ 
                $addRow = true;
                if($idFilter != NULL){      
                    if(isset($row[0])){
                        if(!in_array($row[0], $idFilter)){
                            $addRow = false;
                        }
                    }
                }
                if($addRow){
                    $colIdx = 0;
                    foreach($row as $val){      
                        $array[$rowIdx][$headers[$colIdx]] = $val;
                        $colIdx++;  
                    }
                    $rowIdx++;                      
                }
            }           
            fclose($fh);
            return true;
        } else {
            echo "Unable to open file: ".$path;
        }
    } else {
        echo "CSV doesn't exist: ".$path;
    }
    return false;
}

如果函数返回true,则检查数组是否为null或空,然后对数据进行排序。

if($this->ReadCsvDataIntoArray($client_library_path, $headers, $CSVdata, $log)){
     if($CSVData != NULL){
          usort($CSVdata, create_function('$a, $b', 'return $a["engagement"] < $b["engagement"];'));
      // Do stuff with the sorted array
     } else {
          echo "CSV data is NULL.'n";
     }

我一直得到"CSV数据是NULL"从这个。如果我将逻辑更改为if($CSVData == NULL)甚至if(empty($CSVData)),它将进入if语句,尝试对数组(已满,即使if语句说它是空的)进行排序,并使用数据做东西。

这就是我的第二个问题。这个排序在我的本地主机上运行:

usort($CSVdata, function($a, $b) { return $a["scheduled"] < $b["scheduled"]; });

,但它不能在服务器上工作,因为它的php版本,所以我把它改为:

usort($CSVData, create_function('$a, $b', 'return $a["scheduled"] < $b["scheduled"];'));

但是对于ussort的create_function版本,我得到这个错误消息

Warning: usort(): The argument should be an array 

我猜这与我的完整数组以某种方式被评估为空和null的事实有关,即使它不是

你这么说:

…,并通过引用变量…传递数组返回

:

如果函数返回true,则检查数组是否正确没有作为null或空返回,然后对数据进行排序。

你为什么要这样做?如果您正在检查truefalse,然后检查它是否为nullempty,其值是什么?只需检查它是否为nullempty,而不是这样做:

        // return true;
        return $array;
    } else {
        echo "Unable to open file: ".$path;
    }
} else {
    echo "CSV doesn't exist: ".$path;
}
// return false;
return $array;

然后去掉接口中$array的return by reference:

function ReadCsvDataIntoArray($path, &$headers, $array, $idFilter = NULL){ 

您的truefalse逻辑可能已损坏&根本不值得正确处理。但是,如果值返回nullempty,并且这是您正在执行的操作,那么为什么要花时间重新发明轮子呢?

你也可以调整$CSVData逻辑以适应新的结构:

 $CSVData = $this->ReadCsvDataIntoArray($client_library_path, $headers, $CSVdata, $log);
 if(!empty($CSVData)){
      usort($CSVdata, create_function('$a, $b', 'return $a["engagement"] < $b["engagement"];'));
  // Do stuff with the sorted array
 } else {
      echo "CSV data is empty.'n";
 }

另外,你的整个return true逻辑是严格基于文件本身可以打开:

    $fh = fopen($path, 'r');
    if($fh){
        // Code removed for structural illustration purposes.
        // ...
        // ...
        fclose($fh);
        return true;
    } else {

但你说这个;empahsis我:

我有一个PHP函数,读取CSV数据到一个数组,然后返回

。您的逻辑没有检查数据是否已成功读取。如果文件本身可以读取,则逻辑返回true。这并不意味着文件本身的内容是有效的。你检查过文件本身了吗?或者你检查过这行:

while($row = fgetcsv($fh)){

实际上有值在$row做这样的事情吗?

echo '<pre>';
print_r($row);
echo '</pre>';

我想也许你的CSV有行格式问题。就像它被保存在Windows机器上,但现在正在Mac OS X或Linux机器上读取,或者反过来。查看fgetcsv的文档:

注意:如果PHP在读取时不能正确识别行结尾在Macintosh计算机上或由Macintosh计算机创建的文件,启用Auto_detect_line_endings运行时配置选项可能会有所帮助解决问题

所以也许添加这行启用auto_detect_line_endings到你的函数,像这样:

function ReadCsvDataIntoArray($path, &$headers, &$array, $idFilter = NULL){
    ini_set("auto_detect_line_endings", true);