PHP数组两次爆炸 [英] PHP Array Explode Twice

查看:67
本文介绍了PHP数组两次爆炸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,需要将其插入数据库的多行中.数组的结构如下:

I have an array which I need to insert into multiple rows of database. The structure of array is like:

$var = "Name1,Age1,DOB1,Relation1.Name2,Age2,Dob2,Relation2.";//And so on, depending on users input

(点表示新行,而逗号表示新列) 我需要将其插入数据库:

(Dot indicates new line whereas Comma indicates new column) I need to insert it into database like this:

我首先将所有行存储在一个数组中,例如:

$rowsToInsert = explode (".",$var);

我现在有:

$rowsToInsert[0] = Name1,Age1,DOB1,Relation1;
$rowsToInsert[1] = Name2,Age2,DOB2,Relation2; 
...And So on...

问题:

将这些数组元素存储到具有名称",年龄","DOB"和关系"列的数据库中最快的方法是什么?

What is the fastest way to store these array elements into database having Name, Age, DOB, Relation columns?

推荐答案

可能会起作用

$rows = explode (".",$var);
$addslash = addslashes($rows);

foreach($addslash as $val) {
    $val_str = str_replace("," ,"','", $val);
    $sql = "INSERT INTO tablename (Name, Age, DOB, Relation) VALUES ('" .$val_str. "')";
}

这篇关于PHP数组两次爆炸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆