PHP: 导入 csv 数据的脚本解析错误

PHP: Parse Error in script importing csv data

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

您还需要在回调声明的末尾添加一个分号