[英]Array is empty, shows nothing
我嘗試了dd($ inserts),但顯示為“ []”。 這是我控制器中的代碼。
public function importExcel()
{
$path = Input::file('import_file')->getRealPath();
$inserts = [];
Excel::load($path, function($reader) use ($inserts) {
foreach ($reader->toArray() as $rows) { // <-- $rows pertains to array of rows
foreach($rows as $row) { // <-- $row pertains to the row itself
$inserts[] = ['title' => $row['title'], 'description' => $row['description']];
}
}
});
dd($inserts);
return back();
}
由於似乎您有“行”而不是只有“行”,因此您需要再次遍歷這些行。
Excel::load($path, function($reader) use ($inserts) {
foreach ($reader->toArray() as $rows) { // <-- $rows pertains to array of rows
foreach($rows as $row) { // <-- $row pertains to the row itself
$inserts[] = ['title' => $row['title'], 'description' => $row['description']];
}
}
});
實際上,您可以執行此操作。
$rows = $reader->toArray();
foreach($rows as $row) { // <-- $row pertains to the row itself
$inserts[] = ['title' => $row['title'], 'description' => $row['description']];
}
或者,如果您想堅持使用get
,我認為它會返回一個集合。
$rows = $reader->get();
foreach($rows as $row) {
$inserts[] = ['title' => $row->title, 'description' => $row->description];
}
UPDATE
為了反映$inserts
數組的修改,您需要傳遞其引用。
function($reader) use (&$inserts) {
// ^-- add this to pass its reference.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.