从 .csv 解析的多维数组 -> 逗号导致 mysql 数据库插入问题
我正在使用 Codeigniter 将上传的 csv 文件(这是一个多维数组)解析到数据库中。我已经尝试了一切来正确解析逗号值,但是 mysql 中的“id”列很短,因为它读取“文本”,而不是“文本,文本,文本”。帮助!?
*For reference:*
print_r($data['csvData']);
Array ( [0] => Array ( [category,id] => text1,"text,text,text" )
[1] => Array ( [category,id] => text2,"text,text,text" )
)
foreach($data['csvData'] as $row) {
foreach ($row as $item) {
$item=explode(",", $item);
$results_array = array(
'category' => $item[0],
'id' => $item[1]
);
$this->db->set($results_array);
$this->db->insert('table', $results_array);
}
}
I am using Codeigniter to parse an uploaded csv file (which is a multi-dimensional array) into a database. I have tried everything to parse the comma values correctly, but the "id" column in mysql comes up short, as it reads "text", and not "text,text,text". Help!?
*For reference:*
print_r($data['csvData']);
Array ( [0] => Array ( [category,id] => text1,"text,text,text" )
[1] => Array ( [category,id] => text2,"text,text,text" )
)
foreach($data['csvData'] as $row) {
foreach ($row as $item) {
$item=explode(",", $item);
$results_array = array(
'category' => $item[0],
'id' => $item[1]
);
$this->db->set($results_array);
$this->db->insert('table', $results_array);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我未经教育的猜测:
$item=explode(",", $item);
正在爆炸$item
,即text1,"text,text,text"< /代码>,对吧?所以它看到 4 个逗号,然后将它们分解。因此
$item[0]
将是“text1,$item[1]
将是“text”$item[2]
将是“ text”和$item[3]
将是“text”。您可以尝试将 csv 中的分隔符设置为逗号以外的其他内容,然后将其分解。
或者您可以在之前连接其他项目将它们插入数据库:
My uneducated guess:
$item=explode(",", $item);
is exploding$item
which istext1,"text,text,text"
, right? So it sees 4 commas, and explodes them. Therefore$item[0]
will be "text1,$item[1]
will be "text"$item[2]
will be "text" and$item[3]
will be "text".You can try to set your delimiter in the csv as something other than a comma, and explode that.
Or you can concatenate the other items before inserting them into the db: