2012-03-02 235 views
0
我有一個很難通過以下陣列循環

-循環通過多陣列

array("newProduct"=>array('rmsId' => '123456789','departmentName' => 'Britax','categoryName' => 'Car Seat','productName' => 'Test Product For Britax Car Seat','productPrice' => 33.99,'productQty' => 10), 
     "newProduct"=>array('rmsId' => '987654321','departmentName' => 'Stokke','categoryName' => 'Highchairs','productName' => 'Test Product For Stokke Highchair','productPrice' => 83.99,'productQty' => 20), 
     "newProduct"=>array('rmsId' => '13579','departmentName' => 'Maclaren','categoryName' => 'Buggy','productName' => 'Test Product For Maclaren Buggy','productPrice' => 183.99,'productQty' => 5) 
       ); 

有人可以把我關在正確的軌道,以實現這一目標呢?我的目標是每個加入到MySQL數據庫

感謝

回答

3

您陣列是無效的;一個鍵只能存在一次。您應該可以創建數字索引,然後使用foreach()進行循環。

1

首先你需要給每個數組鍵一個唯一的標識符。然後你可以循環它們。

$items = array(
       "product1"=>array('rmsId' => '123456789','departmentName' => 'Britax','categoryName' => 'Car Seat','productName' => 'Test Product For Britax Car Seat','productPrice' => 33.99,'productQty' => 10), 
       "product2"=>array('rmsId' => '987654321','departmentName' => 'Stokke','categoryName' => 'Highchairs','productName' => 'Test Product For Stokke Highchair','productPrice' => 83.99,'productQty' => 20), 
       "product3"=>array('rmsId' => '13579','departmentName' => 'Maclaren','categoryName' => 'Buggy','productName' => 'Test Product For Maclaren Buggy','productPrice' => 183.99,'productQty' => 5) 
       ); 

foreach($items as $product) { 
    echo $product['rmsId']; 
    echo $product['departmentName']; 
    // ... and so on 
} 
1

試試這個: -

<?php 
$var = array("0"=>array('rmsId' => '123456789','departmentName' => 'Britax','categoryName' => 'Car Seat','productName' => 'Test Product For Britax Car Seat','productPrice' => 33.99,'productQty' => 10), 
     "1"=>array('rmsId' => '987654321','departmentName' => 'Stokke','categoryName' => 'Highchairs','productName' => 'Test Product For Stokke Highchair','productPrice' => 83.99,'productQty' => 20), 
     "2"=>array('rmsId' => '13579','departmentName' => 'Maclaren','categoryName' => 'Buggy','productName' => 'Test Product For Maclaren Buggy','productPrice' => 183.99,'productQty' => 5) 
       ); 
      foreach ($var as $key=>$val) { 
      $id = $val['rmsId']; 
      $dname = $val['departmentName']; 
      $cname = $val['categoryName']; 
      $pname = $val['productName']; 
      $price = $val['productPrice']; 
      $qnt = $val['productQty']; 
     $query = mysql_query("INSERT INTO TABLENAME (rmsId,departmentName,categoryName,productName,productPrice,productQty) VALUE(".$id.",".$dname.",".$cname.",".$pname.",".$price.",".$qnt.")"); 
      } 
?> 
1
$array = array(array('rmsId' => '123456789','departmentName' => 'Britax','categoryName' => 'Car Seat','productName' => 'Test Product For Britax Car Seat','productPrice' => 33.99,'productQty' => 10), 
     array('rmsId' => '987654321','departmentName' => 'Stokke','categoryName' => 'Highchairs','productName' => 'Test Product For Stokke Highchair','productPrice' => 83.99,'productQty' => 20), 
     array('rmsId' => '13579','departmentName' => 'Maclaren','categoryName' => 'Buggy','productName' => 'Test Product For Maclaren Buggy','productPrice' => 183.99,'productQty' => 5) 
       ); 


foreach($array as $value){ 
    $query = "insert into (columnname,columnname) values (".$value['rmsId'].",'".$value['departmentName']."')"; 
    //fire query 

} 

假設你的陣列格式如圖 你的陣列stucture是不妥當的,儘量如圖所示