如何将包含字符串的 .txt 文件读取到 laravel 中的数组

how to read .txt file which contains string to an array in laravel

我想读取包含字符串的 .txt 文件并将其转换为数组,为了更好地显示,我将 .txt 中的字符串复制到这里。

Scan Date,Date,Time,PIN,Number,Name,Position,Department,Office,Verification,I/O,Workcode,SN,Machine

02-05-2017 08:45:15,02-05-2017,8:45:15 AM,1,,MIRACLE,,,,1,1,0,13061016420031,Mesin 3
02-05-2017 08:50:17,02-05-2017,8:50:17 AM,1,,MIRACLE,,,,1,2,0,13061016420031,Mesin 3
02-05-2017 08:58:14,02-05-2017,8:58:14 AM,1,,MIRACLE,,,,1,1,0,13061016420031,Mesin 3
02-05-2017 09:00:49,02-05-2017,9:00:49 AM,1,,MIRACLE,,,,1,2,0,13061016420031,Mesin 3
02-05-2017 08:53:21,02-05-2017,8:53:21 AM,2,,LUNA,,,,1,1,0,13061016420031,Mesin 3
02-05-2017 08:54:48,02-05-2017,8:54:48 AM,2,,LUNA,,,,1,2,0,13061016420031,Mesin 3
02-05-2017 08:58:29,02-05-2017,8:58:29 AM,2,,LUNA,,,,1,0,0,13061016420031,Mesin 3
02-05-2017 09:00:55,02-05-2017,9:00:55 AM,2,,LUNA,,,,1,2,0,13061016420031,Mesin 3
03-04-2017 07:59:41,03-04-2017,7:59:41 AM,15,,,,,,1,0,0,13061016420031,Mesin 3
05-04-2017 07:57:40,05-04-2017,7:57:40 AM,15,,,,,,1,0,0,13061016420031,Mesin 3
07-04-2017 08:26:50,07-04-2017,8:26:50 AM,15,,,,,,1,1,0,13061016420031,Mesin 3
10-04-2017 08:08:00,10-04-2017,8:08:00 AM,15,,,,,,1,0,0,13061016420031,Mesin 3
03-04-2017 07:58:38,03-04-2017,7:58:38 AM,16,,,,,,1,1,0,13061016420031,Mesin 3
03-04-2017 18:43:33,03-04-2017,6:43:33 PM,16,,,,,,1,2,0,13061016420031,Mesin 3
04-04-2017 08:56:44,04-04-2017,8:56:44 AM,16,,,,,,1,0,0,13061016420031,Mesin 3
04-04-2017 17:14:18,04-04-2017,5:14:18 PM,16,,,,,,1,2,0,13061016420031,Mesin 3
05-04-2017 07:52:24,05-04-2017,7:52:24 AM,16,,,,,,1,1,0,13061016420031,Mesin 3
05-04-2017 17:43:59,05-04-2017,5:43:59 PM,16,,,,,,1,2,0,13061016420031,Mesin 3
06-04-2017 08:13:05,06-04-2017,8:13:05 AM,16,,,,,,1,1,0,13061016420031,Mesin 3
06-04-2017 17:55:30,06-04-2017,5:55:30 PM,16,,,,,,1,2,0,13061016420031,Mesin 3
07-04-2017 08:12:56,07-04-2017,8:12:56 AM,16,,,,,,1,1,0,13061016420031,Mesin 3
07-04-2017 17:23:48,07-04-2017,5:23:48 PM,16,,,,,,1,2,0,13061016420031,Mesin 3
10-04-2017 08:08:07,10-04-2017,8:08:07 AM,16,,,,,,1,1,0,13061016420031,Mesin 3
10-04-2017 17:14:57,10-04-2017,5:14:57 PM,16,,,,,,1,2,0,13061016420031,Mesin 3
11-04-2017 08:03:15,11-04-2017,8:03:15 AM,16,,,,,,1,1,0,13061016420031,Mesin 3
11-04-2017 17:11:40,11-04-2017,5:11:40 PM,16,,,,,,1,2,0,13061016420031,Mesin 3
12-04-2017 08:03:20,12-04-2017,8:03:20 AM,16,,,,,,1,1,0,13061016420031,Mesin 3
12-04-2017 19:51:34,12-04-2017,7:51:34 PM,16,,,,,,1,2,0,13061016420031,Mesin 3
12-04-2017 20:11:38,12-04-2017,8:11:38 PM,16,,,,,,1,2,0,13061016420031,Mesin 3
17-04-2017 08:23:16,17-04-2017,8:23:16 AM,16,,,,,,1,1,0,13061016420031,Mesin 3
17-04-2017 11:18:01,17-04-2017,11:18:01 AM,16,,,,,,1,1,0,13061016420031,Mesin 3
17-04-2017 11:19:27,17-04-2017,11:19:27 AM,16,,,,,,1,0,0,13061016420031,Mesin 3
18-04-2017 07:59:54,18-04-2017,7:59:54 AM,16,,,,,,1,1,0,13061016420031,Mesin 3
20-04-2017 17:08:21,20-04-2017,5:08:21 PM,16,,,,,,1,2,0,13061016420031,Mesin 3
21-04-2017 08:06:40,21-04-2017,8:06:40 AM,16,,,,,,1,1,0,13061016420031,Mesin 3
21-04-2017 18:47:21,21-04-2017,6:47:21 PM,16,,,,,,1,2,0,13061016420031,Mesin 3
25-04-2017 08:07:34,25-04-2017,8:07:34 AM,16,,,,,,1,1,0,13061016420031,Mesin 3

我已经成功地将它作为一个字符串读取,但我很困惑如何将它变成一个数组并每行读取一个字符串。我想将字符串数据上传到数据库。

我已经看了资料如下,

$input = Input::all();
$file = File::get($input['uploadFile']);

并且结果是字符串,问题 是如何使其每行成为一个数组,以便得到所需的结果:

[
 ["scandate" => '02-05-2017', "Date" => '02-05-2017', "Time" => '08:45:15', 
  "Number" => 1, "Name" => 'MIRACLE'],
 ["scandate" => '02-05-2017', "Date" => '02-05-2017', "Time" => '08:45:15', 
  "Number" => 1, "Name" => 'MIRACLE']
] etc

或至少像这样

[
 ['02-05-2017 08:45:15','02-05-2017','8:45:15AM',1,'','MIRACLE','','','',1,1,0,13061016420031,'Mesin 3'],
 ['02-05-2017 08:45:15','02-05-2017','8:45:15AM',1,'','MIRACLE','','','',1,1,0,13061016420031,'Mesin 3'],

]
foreach (explode("\n", $file) as $key=>$line){
    $array[$key] = explode(',', $line);
}

仅仅因为你有一个空行和标题行,所以它看起来像这样。你可以从数组中删除它。 但是如果我理解正确的话,你应该做的是 import CSV 否则你会在不同类型的输入上遇到很多麻烦。