2014-02-07 16 views
0

我使用SplFileObject解析大CSV文件未定义抵消。该CSV有近100,000个记录和多列。使用列表()

一些列都是空的。

我有以下代码:

$file = new SplFileObject($uploadedFile); 

$file->setFlags(SplFileObject::READ_CSV); 

// ignore the header 
$it = new LimitIterator($file, 1); 

foreach ($it as $row) { 
    list(
     $email, 
     $name) = $row; 
} 

当我运行该脚本,我总是得到一个错误:

PHP Notice: Undefined offset: 1 in script.php on line 5

PHP Notice: Undefined offset: 2 in script.php on line 5

............ 

PHP Notice: Undefined offset: 35 in script.php on line 5

5号线是实际list() = $row

有没有办法我可以修复这个?也许通过检查数组是否有值?

感谢

回答

1

我想通过手动检查是否存在数据例如做到这一点,通过

foreach ($it as $row) { 
    if(isset($row[0])) { 
     $email = $row[0]; 
    } else { 
     // if you want to handle case where email is not present, just do something here 
    } 
    if(isset($row[1])) { 
     $name = $row[1]; 
    } 

    // now here you have $email and $name set to values from the array. 
} 

,如果您有数字索引。

这将使你更严格地控​​制了所要分析的格式,并在出现问题时,更快地调试确切位置的值丢失或以其他方式遵循的逻辑。

+0

我会做,虽然每场? –

+0

你有数字索引,或者需要与像'$行[“NYA”]'东西来访问?你知道这些索引是否存在,还是它们改变了? – Smar

+0

不是'list()'创建数字索引吗? –