简体   繁体   English

将excel文件上传到数据库时如何修复“非法字符串偏移”

[英]How fix the "llegal string offset" while uploading excel file into database

I am trying to import an Excel file into my table using the MaatWebsite package .我正在尝试使用MaatWebsite 包将 Excel 文件导入到我的表中。 But every time I get errors like these:但每次我收到这样的错误时:

Illegal string offset 'Emp'非法字符串偏移量 'Emp'

I am following this tutorial .我正在关注本教程 And here is the code I have tried:这是我尝试过的代码:

 $path = $request->file('attendance_data')->getRealPath();

 $data = Excel::load($path)->get();

 if($data->count() > 0)
 {
     foreach($data->toArray() as $key => $value)
     {
         foreach($value as $row)
         {
             $insert_data[] = array(
                 'employee_card'  => $row['Emp'],
                 'attendance_date'   => $row['Date'],
                 'attendance_time'    => $row['On'],
             );
         }
     }

     if(!empty($insert_data))
     {
         DB::table('attendance_logs')->insert($insert_data);
     }

Any help will be appreciated任何帮助将不胜感激

I can't comment so I've posted this as an answer.我无法发表评论,所以我已将此作为答案发布。

Have you tried to die and dump the $row to check that it is an array of key => value pairs as you're expecting?您是否尝试过死并转储$row以检查它是否是您期望的key => value对数组? The error looks to show up when trying to access a key of an array with numeric keys.尝试使用数字键访问数组的键时似乎会出现该错误。 This could also happen if $row is a string, as you can access string characters using a numeric index similar to an array.如果$row是字符串,也可能发生这种情况,因为您可以使用类似于数组的数字索引访问字符串字符。

$path = $request->file('attendance_data')->getRealPath();

$data = Excel::load($path)->get();

if($data->count() > 0) {

  foreach ($data->toArray() as $key => $value) {

    foreach ($value as $row) {

      // Print the data of $row to see what it actually is
      // and kill the process
      dd($row);

      $insert_data[] = array(
        'employee_card' => $row['Emp'],
        'attendance_date' => $row['Date'],
        'attendance_time' => $row['On'],
      );

    }

  }

  if (!empty($insert_data)) {

    DB::table('attendance_logs')->insert($insert_data);

  }

}

This answer looks to be related.这个答案看起来是相关的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CodeIgniter中的非法字符串偏移量“名称” - llegal string offset 'name' in CodeIgniter 我该如何解决此错误:非法字符串偏移量'text'? - How can i solve this error: llegal string offset 'text'? 第594行的C:\\ xampp \\ htdocs \\ 2 \\ 2 \\ mvc \\ model \\ includes \\ catclass.php中的非法字符串偏移'tittle' - llegal string offset 'tittle' in C:\xampp\htdocs\2\2\mvc\model\includes\catclass.php on line 594 上传到数据库时如何过滤CSV(Excel文件) - how to filter csv (excel file) when uploading to database 上传顶部空白单元格的CSV文件时出现未定义的偏移量错误 - Undefined offset Error while uploading CSV File with Blank Cells at Top 如何修复 php 中的“非法字符串偏移”错误? - How to fix this `Illegal string offset` error in php? 如何修复 PHP 中的警告非法字符串偏移 - How to fix Warning Illegal string offset in PHP 如果用户已经存在于PHP中,则在将Excel文件数据上传到数据库时显示错误消息 - Show the error message while uploading Excel file data to database if user already exist in PHP 上传要导入数据库cakephp的Excel文件时出现问题 - problems uploading an Excel file to be imported into the database cakephp 如何解决“警告:非法字符串偏移” - How to fix “Warning: illegal string offset”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM