1

I have created this small script in order to parse a cvs file and then pass it on to mysql. I need to do quite some editing to it but I just started with editing the format of the date according to the http://csv.thephpleague.com manual.

<?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');        

    ?>

When I try to run this I get an:

Parse error: parse error in /Users/nefeli/Code/ph/script.php on line 18

error code. I am not sure what the problem syntax error is. Any insight on this?

0

1 Answer 1

2

This line has a syntax error:

$reality->fetchAssoc( , $functionHRT());

According to the docs this is the method's signature:

fetchAssoc($offset_or_keys = 0, callable $callable = null)

So, if you want to pass the callback you have to specify also the first parameter to the function. You can use the default (0) and pass the callback as the second parameter.

Also remember that when you're passing the callback you don't have to call it (using the parentheses like you're doing) but you only need to specify the variable, as you've previously declared the lamba function and stored it in the $functionHRT variable

So your call should be:

$reality->fetchAssoc( 0, $functionHRT );

Another error is in the statement:

$functionHRT = function ($row) {
    $row['hrtData'] => DateTimeImmutable::createFromFormat($row['Contract Start'], 'd-m-Y'); /*this is line 18*/
}

You're using the wrong operator to assign a value to the array. It should be:

$functionHRT = function ($row) {
    $row['hrtData'] = DateTimeImmutable::createFromFormat($row['Contract Start'], 'd-m-Y'); /*this is line 18*/     
};

You need also to add a semicolon at the end of the callback's declaration

Sign up to request clarification or add additional context in comments.

5 Comments

thanks a lot for noticing! Do you have any ideas about the parsing error though?
@Byte_Monster: In PHP you can only leave out function arguments when they have a default value and come last. fetchAssoc(, $callback) is therefore not possible. You have to fill in the first argument. fetchAssoc(), fetchAssoc(1) or fetchAssoc(0, $callback) are possible. Second, you don't add parentheses to a callback.
thanks a lot I fixed this. But please stay with me for a moment because I must have made a mistake while using League. I run the composer just as the Docks suggested $ composer require league/csv But now I am getting the error: Fatal error: Class 'League\Csv\Reader' not found in /Users/me/Code/script.php on line 12 I am on Mac os and the composer did run correctly. Am I doing something wrong with the syntax?
actually I found the issue: I did not use include 'vendor/autoload.php'; . Thanks a lot people! this was extremely useful.
@Byte_Monster : to accept an answer you should click on the 'check mark' under the up/downvote arrows :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.