我有一个要插入到数据库表中的 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/