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.
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
You need to dig on the 2nd level of your arrays.
One solution:
foreach($tmp as $v){
echo "(".implode("),(",$v).")";
}