1 回答

TA貢獻1963條經(jīng)驗 獲得超6個贊
不確定 Maat/Laravel Excel 是否可以輕松解決這個問題。所以,我繼續(xù)創(chuàng)建關(guān)聯(lián)數(shù)組,鍵是兩列的連接,我不想在 Excel 中重復(fù)。
然后我使用 foreach 循環(huán)手動檢查,如果關(guān)聯(lián)數(shù)組中存在鍵,則意味著 Excel 中存在重復(fù)條目。
一些示例代碼供參考如下:
$array = Excel::toArray(new MyExcelImport(), request()->file);
$assoc_array = array();
foreach ($array[0] as $key => $value) {
$new_key = $value['company_code'] . $value['clerk_code'];
// Presence of combination of company_code and clerk_code in the assoc_array indicates that
// there is duplicate entry in the Excel being imported. So, abort the process and report this to user.
if (array_key_exists($new_key, $assoc_array)) {
return response()->json("Combination of company_code: " .
$value['company_code'] .
" and clerk_code: " .
$value['clerk_code'] .
" is duplicate in the file being imported. Please correct same and retry upload.", 422);
}
$assoc_array[$new_key] = $value;
}
希望這可以幫助有類似需求的人!
- 1 回答
- 0 關(guān)注
- 201 瀏覽
添加回答
舉報