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

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 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大