php - 拉维尔 5.5 : array_combine(): Both parameters should have an equal number of elements

标签 php mysql laravel laravel-5 laravel-5.5

我有一个要插入到数据库表中的 csv,我删除了标题,以便在我使用 foreach 获取行值时可以将其合并为引用。数据确实插入到数据库表中,但会抛出一个错误,提示 both parameters should have an equal number of elements in the array_combined 。我检查了一行中的标题数组和 dd(),元素编号相同。但是在我完成发布后错误仍然存​​在。

$rows = array_map('str_getcsv', explode("\n",$csvData) );
$header = array_shift($rows);

      foreach($rows as $row )
        {
                $row = array_combine($header, $row);
                People::create([
                    'name' => $row['Name'],
                    'surname' => $row['Surname'],
                    'age' => $row['Age'],
                    'sex' => $row['Sex'],
                    'note' => $row['Note'],
                    'list_id' => $list_num
                ]);
        }

最佳答案

我认为问题可能出在最后一行。

您可以验证替换的确切问题是什么:

$row = array_combine($header, $row);

与:

if (count($header) != count($row)) {
  dd($header, $row);
}

在您的情况下,在跟踪问题后,您可以简单地跳过这种情况下的行,例如:

if (count($header) != count($row)) {
  continue;
}

关于php - 拉维尔 5.5 : array_combine(): Both parameters should have an equal number of elements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47209071/

相关文章:

php - mysql:复制表中的某些行,更改重复行中的信息

mysql - 获取 PostgreSQL bool 字段时出现范围错误

mysql - Pentaho Kettle 脚本选项

sql - 如何在启用 softDeletes 时定义唯一索引

php - 插入数据库时​​未定义索引

php - 全局搜索字段搜索多列

php - 从数组创建 PHP 表

laravel - javascript 和 css 文件在 laravel 项目中的位置

Laravel:如何对忽略语言文章的 Eloquent 集合进行排序?

php - is_array for MySQL Select Statement with PHP