2013-08-21 52 views
-1

我試圖破滅與人物)(多維數組,所以 我可以使用類似這樣的查詢:PHP - 內爆多維數組的MySQL

INSERT INTO (a',b',c) values('a','b','c'),('d','e','f');

這裏是多維數組的一個例子:

$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')); 

這不會產生任何結果,print implode("),(",$tmp);

我不是一個專業的PHP開發人員,也許有做一個簡單的方法這個。 欣賞您的輸入。

+3

破滅適用於簡單的數組。你將不得不在循環中崩潰。 –

回答

0

試試這個:只有

$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 
+0

謝謝。我猜第一個foreach循環通過數組鍵。 – user2704180

1

你需要挖掘你的數組的第二級。

一個解決方案:

foreach($tmp as $v){ 
echo "(".implode("),(",$v).")"; 
} 
+0

這給了我相同的結果:for($ i = 0; $ i user2704180