多维数组入库问题
程序员文章站
2022-05-17 16:02:37
...
print_r($dbinsert)显示内容:
Array
(
[0] => Array
(
[apmac] => 0q:4e:cb:00:01:09
[pub_ip] => 11.24.24.7
[priv_ip] => 12.18.1.3
[hwver] => VA500
[fwver] => 69.0.0
[vsmip] => 12.25.1.19
[conntime] => 677822
[boottime] => 49950
[state] =>
[statetime] => 2013-7-12 18:34:5
[hostname] => name1
[location] => 0004.003.01
[admin_id] => 0
)
[1] => Array
(
[apmac] => 10:qe:cv:02:00:37
[pub_ip] => 11.24.24.7
[priv_ip] => 12.18.1.3
[hwver] => VA500
[fwver] => 69.0.0
[vsmip] => 12.25.1.19
[conntime] => 169547
[boottime] => 403315
[state] =>
[statetime] => 2013-7-12 18:34:5
[hostname] => nam2
[location] => 0004.003.01
[admin_id] => 0
)
[2] => Array
(
[apmac] => r0:ve:cb:00:ff:04
[pub_ip] => 13.24.28.7
[priv_ip] => 92.18.1.18
[hwver] => V
[fwver] => 6.)
[vsmip] => 12.25.1.79
[conntime] => 168
[boottime] => 144497
[state] =>
[statetime] => 2013-7-12 18:34:5
[hostname] => name5
[location] => 0004.003.01
[admin_id] => 0
)
)
问题:
我要把这些内容插入数据库表table3中,表结构跟返回的内容一样且顺序也一样
请问该如何写php程序呢?
抱歉看不懂。。。。能写通俗点吗,在不考虑效率和简介的情况下
Array
(
[0] => Array
(
[apmac] => 0q:4e:cb:00:01:09
[pub_ip] => 11.24.24.7
[priv_ip] => 12.18.1.3
[hwver] => VA500
[fwver] => 69.0.0
[vsmip] => 12.25.1.19
[conntime] => 677822
[boottime] => 49950
[state] =>
[statetime] => 2013-7-12 18:34:5
[hostname] => name1
[location] => 0004.003.01
[admin_id] => 0
)
[1] => Array
(
[apmac] => 10:qe:cv:02:00:37
[pub_ip] => 11.24.24.7
[priv_ip] => 12.18.1.3
[hwver] => VA500
[fwver] => 69.0.0
[vsmip] => 12.25.1.19
[conntime] => 169547
[boottime] => 403315
[state] =>
[statetime] => 2013-7-12 18:34:5
[hostname] => nam2
[location] => 0004.003.01
[admin_id] => 0
)
[2] => Array
(
[apmac] => r0:ve:cb:00:ff:04
[pub_ip] => 13.24.28.7
[priv_ip] => 92.18.1.18
[hwver] => V
[fwver] => 6.)
[vsmip] => 12.25.1.79
[conntime] => 168
[boottime] => 144497
[state] =>
[statetime] => 2013-7-12 18:34:5
[hostname] => name5
[location] => 0004.003.01
[admin_id] => 0
)
)
问题:
我要把这些内容插入数据库表table3中,表结构跟返回的内容一样且顺序也一样
请问该如何写php程序呢?
回复讨论(解决方案)
$f = '';$v = array()foreach($dbinsert as $row) { if(empty($f)) $f = join(',', $array_key($row)); $v[] = "('" . join("','", $row) . "')";}$sql = "insert into table3 ($f) values " . join(',', $v);//执行 $sql 的语句
$f = '';$v = array()foreach($dbinsert as $row) { if(empty($f)) $f = join(',', $array_key($row)); $v[] = "('" . join("','", $row) . "')";}$sql = "insert into table3 ($f) values " . join(',', $v);//执行 $sql 的语句
抱歉看不懂。。。。能写通俗点吗,在不考虑效率和简介的情况下
1.遍历二维数组,将数组里面的元素合并成字符串(中间使用,号),目的是组成一个inset的串
这个不明白?
2.把上面的串和sql语句再组合成inset语句,循环添加即可
版本很牛B,膜拜一下,我只是翻译一下
Notice: Undefined variable: array_key in Dex3.php on line 19 Fatal error: Function name must be a string in Dex3.php on line 19
19 if(empty($f)) $f = join(',', $array_key($row));
if(empty($f)) $f = join(',', array_keys($row));
1.遍历二维数组,将数组里面的元素合并成字符串(中间使用,号),目的是组成一个inset的串
这个不明白?
2.把上面的串和sql语句再组合成inset语句,循环添加即可
版本很牛B,膜拜一下,我只是翻译一下
果然是三哥,聪明