且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP注意:从.csv导入时出现未定义的偏移量错误

更新时间:2022-12-07 07:54:23

由于list()试图分配两个变量,因此它试图访问两个数组元素[0][1].因为该行上没有逗号,所以[1]不存在.

Since list() is attempting to assign two variables it is attempting to access two array elements [0] and [1]. Because there is no comma on the line, [1] does not exist.

尝试一些不同的功能:

$csv_data = file("data3_received.csv", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

foreach($csv_data as $line) {
    $data[] = str_getcsv($line);
}

要获取单个变量,您需要检查是否有多于一列或类似内容:

To get individual variables you need to check if you have more than one column, or something similar:

if(count($data = str_getcsv($line)) > 1) {
    list($service_id, $ki3) = $data;
} else {
    $service_id = $data[0];
}