php - Laravel excel 验证两列的组合是否重复

标签 php laravel laravel-5.8 maatwebsite-excel laravel-excel

我有一个要求,我需要确保用户上传的 Excel 文件没有重复的行 w.r.t. 2 个特定的列。

例子:

在下面的代码片段中,我想指出第 1 行和第 2 行包含 COMPANY_CODECLERK_CODE 的重复组合:

enter image description here

如果发现这样的重复组合,我想拒绝导入整个文件并让用户知道问题出在哪里。

有什么线索吗?

最佳答案

不确定 Maat/Laravel Excel 是否可以轻松解决这个问题。因此,我继续创建关联数组,其中键作为两列的串联,我不想在 Excel 中重复。

然后我使用 foreach 循环手动检查关联数组中是否存在键,这意味着 Excel 中存在重复条目。

下面的一些示例代码供引用:

        $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;
        }

希望这对有类似需求的人有帮助!

关于php - Laravel excel 验证两列的组合是否重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57071153/

相关文章:

php - 在 Laravel 中将 Eloquent 集合与查询合并?

php - 大流量下的网站PHP+MySQL

php - 在某个字符之后删除字符串的一部分

需要 MySQL 数据库结构建议。从一个表到另外两个表的两个多对多,它们处于一对多关系

php - 使用特定连接的 Laravel 原始查询

mysql - actullay dd() 如何在 laravel 中工作

php - 从多个表中搜索数据 laravel

php - 上传不同属性的多个文件

php - 将结果合并到一个展开的表行中

javascript - 使用 jquery 向同一页面发送 GET 请求