我创建了这个小脚本来解析 cvs 文件,然后将其传递给 mysql。我需要对其进行相当多的编辑,但我只是根据 http://csv.thephpleague.com 开始编辑日期格式。手动的。
<?php
if (!ini_get("auto_detect_line_endings")) {
ini_set("auto_detect_line_endings", '1');
}
use League\Csv\Reader;
$reality = Reader::createFromPath('Workbook3.csv');
$planed = Reader::createFromPath('Workbook4.csv');
/*this function removes the "First name" "Second name" elements and
creates one that actually has the entier name in capital*/
$functionHRT = function ($row) {
$row['hrtData'] => DateTimeImmutable::createFromFormat($row['Contract Start'], 'd-m-Y'); /*this is line 18*/
}
$hrtData = $reality
// ->setOffset(7);
->fetchAssoc( , $functionHRT());
$hrtData[0]['Contract Start']->format('Y-m-d');
?>
当我尝试运行它时,我得到:
Parse error: parse error in /Users/nefeli/Code/ph/script.php on line 18
错误代码。我不确定语法错误是什么问题。对此有任何见解吗?
最佳答案
这一行有一个语法错误:
$reality->fetchAssoc( , $functionHRT());
根据docs这是方法的签名:
fetchAssoc($offset_or_keys = 0, callable $callable = null)
所以,如果你想传递回调,你还必须指定函数的第一个参数。您可以使用默认值 (0) 并将回调作为第二个参数传递。
还请记住,当您传递回调时,您不必调用它(像您正在做的那样使用括号)但您只需要指定变量,因为您之前已经声明了 lamba function并将其存储在 $functionHRT
变量
所以你的电话应该是:
$reality->fetchAssoc( 0, $functionHRT );
语句中还有一个错误:
$functionHRT = function ($row) {
$row['hrtData'] => DateTimeImmutable::createFromFormat($row['Contract Start'], 'd-m-Y'); /*this is line 18*/
}
您使用了错误的运算符来为数组赋值。应该是:
$functionHRT = function ($row) {
$row['hrtData'] = DateTimeImmutable::createFromFormat($row['Contract Start'], 'd-m-Y'); /*this is line 18*/
};
您还需要在回调声明的末尾添加一个分号
关于PHP:脚本导入 csv 数据时出现解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34175833/