所以我正在使用Laravel和Laravel Excel扩展程序。我想导入Excel文件,然后将文件中的所有数据插入MySQL DB。我知道数据库表列的计数和名称,但Excel文件的计数和名称可能不同,所以我希望用户将在DB文件的Excel文件中指定nesseccary列名。例如,我们有一个DB表,其中的列名为" name"," cost"和"描述"所以,如果我有一个包含列" column1"的文件等于" name"我应该填写" name"表格中的字段为" column1"我必须和其他colums做同样的事情,所以我遇到了一个惊喜问题。我的代码中有一条评论:
Excel::load(Input::file('excel_import_file'), function($reader) {
echo '<pre>';
var_dump($reader->toArray());
echo '</pre>';
//$reader->each(function($sheet) {
//echo '<pre>';
//var_dump($sheet->toArray());
//echo '</pre>';
$input = Input::all(); //get all data from the input fields
$fields = [
'subcategory_id' => $input['subcategory_id'],
'seller_id' => Auth::user()->id,
'name' => null,
'description' => null,
'cost' => null,
'currency_id' => $input['currency_id'],
'warranty' => null,
'img' => null,
'date_expiration' => null
]; //prepare nesseccary data for DB table with a foreighn keys (subcategory_id, currency_id are coming from the drop-down lists, so seller_id is comming from the session)
$new_fields = []; //prepare new assocciated array for inserting to DB
foreach($reader->toArray() as $a) { //parsing data
foreach($a as $k => $v) { //parsing each row from the Excel file as key => value
foreach($input as $input_k => $input_v) {
$k == $input_v ? $k = $input_k : $k = $k; //replacing keys in array from the Excel file for correct DB table structure
}
//echo $k . ' ' . $v . '<br>';
foreach($fields as $field_k => $field_v) {
$field_k == $k ? $field_v = $v : $field_v = $field_v; //looking for same keys from $fields array and assigning values for each key which exists in $k
echo $field_k . ' - ' . $field_v . '<br>';
$new_fields = array_merge($new_fields, [$field_k => $field_v]); //I catch a problem here, I want to merge two assoc arrays, but I got array with a first value and a second with a second value
//SAGoodies::insertTo($new_fields);
}
//echo '<pre>';
//var_dump($sheet->toArray());
//echo '</pre>';
echo '<pre>';
var_dump($new_fields);
echo '</pre>';
}
}
//});
});
所以这是一个转储:
subcategory_id - 43
seller_id - 20
name - ololo
description -
cost -
currency_id - 1
warranty -
img -
date_expiration -
array(9) {
["subcategory_id"]=>
string(2) "43"
["seller_id"]=>
int(20)
["name"]=>
string(5) "ololo"
["description"]=>
NULL
["cost"]=>
NULL
["currency_id"]=>
string(1) "1"
["warranty"]=>
NULL
["img"]=>
NULL
["date_expiration"]=>
NULL
}
subcategory_id - 43
seller_id - 20
name -
description -
cost - 333
currency_id - 1
warranty -
img -
date_expiration -
array(9) {
["subcategory_id"]=>
string(2) "43"
["seller_id"]=>
int(20)
["name"]=>
NULL
["description"]=>
NULL
["cost"]=>
float(333)
["currency_id"]=>
string(1) "1"
["warranty"]=>
NULL
["img"]=>
NULL
["date_expiration"]=>
NULL
}
但我希望得到类似的东西:
array(9) {
["subcategory_id"]=>
string(2) "43"
["seller_id"]=>
int(20)
["name"]=>
string(2) "ololo"
["description"]=>
NULL
["cost"]=>
float(333)
["currency_id"]=>
string(1) "1"
["warranty"]=>
NULL
["img"]=>
NULL
["date_expiration"]=>
NULL
}
我猜它发生了,因为在这一行:
$new_fields = array_merge($new_fields, [$field_k => $field_v]);
我们每次都有空$ new_fields。有什么想法吗?!
答案 0 :(得分:0)
所以,我通过Laravel Excel的select方法找到了新的解决方案。它看起来像
Excel::load(Input::file('excel_import_file'), function($reader){
$input = Input::all();
$result = $reader->select(array($input['name'],
$input['description'],
$input['cost'],
$input['warranty'],
$input['img'],
$input['date_expiration']))->limit(500)->get();
$fields = [
'subcategory_id' => $input['subcategory_id'],
'seller_id' => Auth::user()->id,
'currency_id' => $input['currency_id'],
];
$new_array = [];
foreach($result->toArray() as $array) {
$new_array = array_merge($array, $fields);
SAGoodies::insertTo($new_array);
//echo '<pre>';
//var_dump($new_array);
//echo '</pre>';
}
});
这个解决方案有什么想法?!我们从$ result得到的数组中的想法。我不想触及它的一个结构,不想迭代。我只是将$ fields中所需的foreighn键添加到$ result数组中的每个数组。简单而有效的解决方案。
P.S。我需要为表格和检查功能检查加载文件的扩展名,它必须是xls,xlsx或csv并检查文件大小以防止现在加载太大的文件。