douqi2804 2013-08-21 15:28
浏览 51
已采纳

PHP - 用于MySQL的Implode多维数组[关闭]

I'm attempting to implode a multi-dimensional array with the characters ),(, so that i can use a query similar to this:

insert into table temp (a,b,c) values ('a','b','c'),('d','e','f');

Here is an example of the multidimentional array:

    $tmp = array(
              array(0 => 'CompanyB',
                    1 => 'IndustryA',
                    2 => 'Sysadmin',
                    3 => '01',
                    4 => '2011',
                    5 => '12',
                    6 => '2012',
                    7 => 'aeere',
                    8 => 'R6000',
                    9 => 'asdfasdf'),
              array(0 => 'CompanyC',
                    1 => 'IndustryC',
                    2 => 'Aabb',
                    3 => '02',
                    4 => '2012',
                    5 => '01',
                    6 => '2013',
                    7 => 'asdf',
                    8 => 'R7000',
                    9 => 'adfasdfeeeeeeeeeeeeeee'),
              array(0 => 'CompanyD',
                    1 => 'IndustryARR',
                    2 => 'NJNNLK',
                    3 => '01',
                    4 => '2011',
                    5 => '01',
                    6 => '2012',
                    7 => 'Anotheron',
                    8 => 'R9000',
                    9 => 'qweqweqwe'));

this does not produce any result, print implode("),(",$tmp);

I'm not a professional php developer, maybe there's an easier way to do this. Appreciate your input.

  • 写回答

2条回答 默认 最新

  • douhuang5623 2013-08-21 15:37
    关注

    Try this:

    $rows = array(); // will hold all table rows to insert
    foreach ($tmp as $row) { // loops through your datasets
        $row_string = ''; // will hold the string for this row
        foreach ($row as $value) {
            // make sure that value is escaped
            $row_string .= '"' . addslashes($value) . '", ';
        }
        $row_string = '(' . substr($row_string, 0, -2) . ')'; // trim last ", " and wrap in brackets
        $rows[] = $row_string; // push row to rows array
    }
    $rows = implode(', ', $rows); // glue rows together into one string
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。