Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question

closed as off-topic by deceze, davidkonrad, Brian Nickel, Jimbo, tereško Aug 21 '13 at 23:28

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist" – deceze, Jimbo, tereško
If this question can be reworded to fit the rules in the help center, please edit the question.

3  
implode only works on simple arrays. you'll have to implode in a loop. –  Marc B Aug 21 '13 at 15:30

2 Answers 2

up vote 0 down vote accepted

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
share|improve this answer
    
Thank you. I'm guessing the first foreach loop cycles through the array keys. –  user2704180 Aug 22 '13 at 6:30

You need to dig on the 2nd level of your arrays.

One solution:

foreach($tmp as $v){
 echo "(".implode("),(",$v).")";
}
share|improve this answer
    
That gave me same result as: for($i = 0;$i < count($tmp);$i++) { print array_values($tmp[$i]); } –  user2704180 Aug 22 '13 at 6:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.