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

时间:2017-05-08 03:09:07

标签: php arrays string laravel-5 laravel-filesystem

我想阅读.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'],

]

1 个答案:

答案 0 :(得分:2)

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

只是因为你有一个空行和标题行,它会看起来像这样。你可以从数组中删除它。 但如果我理解你应该做的是导入 CSV ,否则你会对不同类型的输入产生很多麻烦。

enter image description here