未定义偏移量:1 Laravel / PHP上传CSV [英] Undefined offset: 1 Laravel/PHP uploading CSV

查看:520
本文介绍了未定义偏移量:1 Laravel / PHP上传CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用软件包> http://csv.thephpleague.com/

I'm using a package > http://csv.thephpleague.com/

但是我得到一个未定义的偏移:1时上传,虽然CSV上传和工作这个错误被抛出,显然我不是客户端看到或认为有一个问题。

But I'm getting a Undefined offset: 1 when uploading, although the CSV does upload and works this error is thrown which obviously I don't the client see or think there's a problem.

这里有问题的方法:

public function postUpload()
{
    $file = Input::file('file');
    $name = time() . '-' . $file->getClientOriginalName();
    $moved = $file->move(public_path() . '/uploads/CSV', $name);

    DB::table('wc_program_1')->truncate();

    $csv = new Reader($moved);
    $csv->setOffset(0); //because we don't want to insert the header
    $nbInsert = $csv->each(function ($row) use (&$sth) {
        DB::table('wc_program_1')->insert(
            array(

                'Road' => $row[0],
                'Parish' => $row[1],
                'WorkType' => $row[2],
                'TrafficManagement' => $row[3],
                'Duration' => $row[4],
                'Start' => $row[5]
            )
        );
        return true;
    });

    return Redirect::to('admin/programmes')->with('flash_success', 'Upload completed & new data inserted.');
}

这里的表结构:

推荐答案

必须有更多的错误吗? 未定义的偏移:line#4323上的somefile.php中有1个或w / e

There must be more to the error? Undefined Offset: 1 in somefile.php on line #4323 or w/e

从数组。

我看到的唯一数组是 $ row 所以 $ row [ 1] 不存在。

The only array I see is $row so $row[1] does not exist.

如果您只想快速修补,我建议这样:

If you just want to quickly patch it then I suggest this:

'Parish' => (isset($row[1]) ? $row[1] : ''),

可能是一个好主意,对所有这样做,例如:

It would probably be a good idea to do this for all of them such as:

'Road' => (isset($row[0]) ? $row[0] : ''),

这篇关于未定义偏移量:1 Laravel / PHP上传CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆