Home>
I'm going to ask you how to get values ​​as an array using the CSV import function in PHP.

I have implemented a function that imports CSV and saves it in the user table.
It is a situation where I want to get the column name of the header part and user information other than the header part by turning in foreach.

Specifically, the following code gets the value by specifying the column name, but the column name is also CSV
I want to get it from the header part.

$user = array (
            'user_id' =>$row [0],
            'account_name' =>$row [1],
            'account_type' =>$row [2],
        );
        return $user;
    }

The following code is currently implemented.

public function upload (Request $request)
    {
        $csv_file = $request->file ('file');
            // Goodby CSV settings
            $config_in = new LexerConfig ();
            $config_in
                ->setDelimiter (",")
                ->setFromCharset ("SJIS-win")
                ->setToCharset ("UTF-8")
            ;
            $lexer_in = new Lexer ($config_in);

            $data = array ();
            $interpreter = new Interpreter ();
            $interpreter->addObserver (function (array $rows) use (&$datalist) {
                // get data for each column
                $data [] = $rows;
            });
            // Parse CSV data
            $lexer_in->parse ($csv_file, $interpreter);
            // delete TMP file
            unlink ($csv_file);
            $csv_header = $data [0];
            $csv_body = array_splice ($data, 1);
            foreach ($csv_body as $rows) {
                // each data fetch
                $csv_user = $this->get_csv_user ($rows, $csv_header);
                // Registration to DB
                $this->register_user_csv ($csv_user);
            }
            return view ('/ upload_complete');
        }
    }
    private function get_csv_user ($rows, $csv_header)
    {
        foreach ($rows as $row) {
            $user = new User ();
            foreach ($row as $key =>$value) {
                $csv_header [$key]->$row [$key];
            }
        }
        return $user;
    }
  private function register_user_csv ($user)
    {
        $new_user = new User;
        foreach ($user as $key =>$value) {
            $new_user->$key = $value;
        }
        $new_user->save ();
    }
}


This implementation is currently in an error state.

User data in $rows as of "$csv_user = $this->get_csv_user ($rows, $csv_header);"
We have confirmed that $csv_header contains header column information.
In addition, if the code presented as a specific example is used, it can also be successfully imported into the User table
It is in a state where it can be confirmed.

I think that it is an elementary content, but I would appreciate it if you could tell me.
Thank you in advance.

Appendix

The details of the error are as follows.
"Invalid argument supplied for foreach ()"
It was an error that occurred in the "foreach ($row as $key =>$value)" part of the get_csv_user method.

I am sorry for the lack of information.

  • Answer # 1

    I guess this is because there is no error information.

    private function get_csv_user ($rows, $csv_header)
        {
            $user = array ();
            foreach ($i = 0;$i


    --- Addendum ---
    How to avoid loops

    private function get_csv_user ($rows, $csv_header)
        {
            return array (
                $csv_header [0] =>$rows [0],
                $csv_header [1] =>$rows [1],
                $csv_header [2] =>$rows [2]);
        }

  • Answer # 2

    I don't understand the intention of the question a little, but do you want to process only the first line of csv?

    When processing csv, as a process in foreach, it is often done to determine the first line with key and make another process.

  • Answer # 3

    Do you want to do this?

    $row) {
        if ($i === 0) {
            continue;
        }
        $arr [] = array_combine ($rows [0], $row);
    }
    var_dump ($arr);