Php 更改函数内的指针值
我使用指针Php 更改函数内的指针值,php,laravel-excel,Php,Laravel Excel,我使用指针$break&$columns\u match来确定Excel文件中的列是否与数据库中的表交叉匹配。每次处理新块时,这些变量的值都会变回false。即使当我试图使变量成为整个类的私有道具时,每次它仍然变回false /** Init new variables **/ $break = false; $columns_match = false; $filename = $request->file->getClientOriginalNam
$break
&$columns\u match
来确定Excel文件中的列是否与数据库中的表交叉匹配。每次处理新块时,这些变量的值都会变回false。即使当我试图使变量成为整个类的私有道具时,每次它仍然变回false
/** Init new variables **/
$break = false;
$columns_match = false;
$filename = $request->file->getClientOriginalName();
/** Insert new log in upload_logs **/
$upload_log = Upload_log::create([
*censored*
]);
/** Move uploaded file to imported files directory **/
$file = $request->file->move(public_path(*censored*));
$path = $file->getRealPath();
Excel::filter('chunk')->load($path)->chunk(1000, function($reader) use($tableName, &$columns_match, &$break) {
var_dump('start:');
var_dump($columns_match);
if (!$break) {
if (!$columns_match) {
/** Truncate destination table before inserting data to prevent duplicates **/
DB::connection(*censored*)
->table($tableName)
->truncate();
/** Get destination table columns **/
$columns = DB::connection(*censored*)
->getSchemaBuilder()
->getColumnListing($tableName);
/** Check if destination table columns are found in uploaded file **/
if ($reader->count()) {
$headerRow = $reader->getHeading();
$columns_match = !array_diff($columns, $headerRow);
var_dump('change:');
var_dump($columns_match);
}
}
/** Check if uploaded file's headers match to destination table columns **/
if ($columns_match) {
/** Extract only the data we need in destination table from uploaded file **/
$data = array_map(function($row) use($columns) {
$arr = [];
foreach ($columns as $column) {
$arr[$column] = $row[$column];
}
return $arr;
}, $reader->toArray());
/** Insert the data to destination table **/
DB::connection(*censored*)
->table($tableName)
->insert($data);
var_dump('end');
var_dump($columns_match);
} else {
$break = true;
}
}
});
上面印着:
string(6) "start:"
bool(false)
string(7) "change:"
bool(true)
string(3) "end"
bool(true)
string(6) "start:"
bool(false)
string(7) "change:"
bool(true)
string(3) "end"
bool(true)
string(6) "start:"
bool(false)
string(7) "change:"
bool(true)
string(3) "end"
bool(true)
有什么问题吗?如果在
use()
中的变量之前没有&
,就会发生这种情况。你确定你在真实的代码中有吗?@Barmar当然,这是我写在文件中的代码的“复制粘贴”,没有修改(显然,除了审查的内容)。我想不出其他任何东西。尝试回显函数中的变量以确保它们被赋值。我已经更新了帖子以显示var_转储。如您所见,在每个块的末尾,布尔值仍然等于true,但在新块的开头将重置为false您确定这是来自同一循环的多个块,而不是对该代码块的多个调用吗?你能用一个你自己在循环中调用的闭包而不是通过Laravel Excel来复制它吗?如果在use()
中的变量之前没有&
,就会发生这种情况。你确定你在真实的代码中有吗?@Barmar当然,这是我写在文件中的代码的“复制粘贴”,没有修改(显然,除了审查的内容)。我想不出其他任何东西。尝试回显函数中的变量以确保它们被赋值。我已经更新了帖子以显示var_转储。如您所见,在每个块的末尾,布尔值仍然等于true,但在新块的开头将重置为false您确定这是来自同一循环的多个块,而不是对该代码块的多个调用吗?你能用一个你自己在循环中调用的闭包而不是通过Laravel Excel来复制它吗?