PHP:脚本导入 csv 数据时出现解析错误

标签 php mysql csv import-from-excel

我创建了这个小脚本来解析 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/

相关文章:

javascript - 通过AJAX结果可见数据集

sql - MYSQL join - 从嵌套选择引用外部字段?

java - CSV 文件在 Java 中无法正确写入

php - 运行以下 PHP/MySQL/Linux 系统所需的硬件

php - 删除 php-mysql 结果行中的重复字段

php - 身份验证返回 false Laravel 4

mysql - SQL查询查找每个零件价格最低的供应商

mysql - 表之间的内连接,耗时

json - neo4j 浏览器导出文件,包括关系

python - 如何使用 Pandas 将csv转换为字典