2010-07-29 133 views
2

我怎麼能解析PHP數組是這樣的:PHP - 重新格式化多維數組插入MYSQL?

$cars= array(
    "Ford"=>array("C-Max"=>array("length"=>"4333","width"=>"1825","height"=>"1560"),"Escape"=>array("length"=>"4480","width"=>"1845","height"=>"1730") 
    ,"Explorer"=>array("length"=>"4912","width"=>"1872","height"=>"1849"),"Fiesta"=>array("length"=>"3950","width"=>"1973","height"=>"1433") 
    ,"Focus"=>array("length"=>"4488","width"=>"1840","height"=>"1497"),"Fusion"=>array("length"=>"4013","width"=>"1724","height"=>"1543") 
    ,"Galaxy"=>array("length"=>"4820","width"=>"1854","height"=>"1723"),"Kuga"=>array("length"=>"4443","width"=>"1842","height"=>"1677") 
    ,"Mondeo"=>array("length"=>"4844","width"=>"1886","height"=>"1500"),"Ranger"=>array("length"=>"5075","width"=>"1805","height"=>"1745") 
    ,"S-Max"=>array("length"=>"4768","width"=>"1854","height"=>"1658"), 
    "Hummer"=>array("H2"=>array("length"=>"5170","width"=>"2063","height"=>"2012"),"H3"=>array("length"=>"4782","width"=>"1989","height"=>"1872"))); 

插入到MySQL表是這樣的:

CREATE TABLE IF NOT EXISTS `cars_dimensions` (
    `id` int(10) NOT NULL auto_increment, 
    `brand` varchar(120) character set utf8 NOT NULL, 
    `model` varchar(120) character set utf8 NOT NULL, 
    `length` varchar(5) character set utf8 NOT NULL, 
    `width` varchar(5) character set utf8 NOT NULL, 
    `height` varchar(5) character set utf8 NOT NULL, 
    KEY `id` (`id`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ; 

當我使用foreach和像$cars["Hummer"]["H2"]["length"];我有點不能讓另一個數組維和同時緩存實際的品牌/型號...... Tho,我的實際陣列在第一維(品牌)中約有3000個項目。

+0

到目前爲止,每一個答案都假設悍馬與福特在同一水平,但在你的例子中它不是,也許這只是一個錯字。如果這是他們的代碼將起作用,如果不是迄今爲止的3個答案都不適合。 – 2010-07-29 11:59:49

+0

是的,對不起,我只是以錯誤的方式複製代碼:) – user405610 2010-07-29 12:13:57

回答

4

你需要兩個循環,一個是品牌,一個是他們的模型:

foreach ($cars as $brand => $models) { 
    foreach ($models as $model => $specs) { 
     $query = "INSERT INTO cars_demensions (brand, model, length, weight, height) 
        VALUES ('$brand', '$model', {$specs['length']}, {$specs['width']}, {$specs['height']});"; 
    } 
} 
1
foreach ($cars as $brandname => $carinfo ) 
{ 
foreach ($carinfo as $modelname => $car) 
{ 
// brand = $brandname 
// model = $modelname 
// length = $car['length'] 
// do insert query here 
} 
} 
1

$rows = ''; 
foreach($cars AS $brand) { 
    foreach($brand AS $model) { 
    if(!empty($rows)) $rows .= ', '; 
    $rows = "({$car['width']},...)"; 
    } 
} 

$sql = "INSERT INTO cars_dimensions (width,...) VALUES $rows"; 
+0

+1擴展查詢。 – 2010-07-29 12:06:41

0

完整的可執行代碼和TESTED。這是更高效,更快捷的方式。使用這種方法,您可以使用單個插入查詢插入多行。

<?php 
$col = []; 
foreach ($cars as $brand => $models) { 
    foreach ($models as $model => $specs) { 
     if (isset($specs['length']) || isset($specs['width']) || isset($specs['height'])) { 
      $length = $specs['length']; 
      $width = $specs['width']; 
      $height = $specs['height']; 
     } else { 
      foreach ($specs as $k => $v) { 
       $length = $v['length']; 
       $width = $v['width']; 
       $height = $v['height']; 
      } 
     } 
     $col[] = "('" . $brand . "', '" . $model . "', '" . $length . "', '" . $width . "', '" . $height . "')"; 
    } 
} 
$query = "INSERT INTO `cars_dimensions`(`brand`, `model`, `length`, `width`, `height`) VALUES" . implode(',', $col) . ";"; 

$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "test"; 
try { 
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); 
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    $conn->exec($query); 
} catch (PDOException $e) { 
    echo $e->getMessage(); 
} 
?> 

希望它能幫助你。謝謝。