Home > Backend Development > PHP Tutorial > 多维数组入库问题

多维数组入库问题

WBOY
Release: 2016-06-23 14:15:25
Original
943 people have browsed it

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程序呢?


回复讨论(解决方案)

$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 的语句
Copy after login
Copy after login

$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 的语句
Copy after login
Copy after login



抱歉看不懂。。。。能写通俗点吗,在不考虑效率和简介的情况下

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,膜拜一下,我只是翻译一下

果然是三哥,聪明

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template