2013-02-27 81 views
0

我想從頭部作爲關鍵字的csv文件中創建一個數組。但是,當我將腳本插入到數據庫中時,它將覆蓋csv文件的前一行,並且只有最後一個值存儲在數據庫中。csv到數組覆蓋數據

我認爲它是因爲數組的默認值爲5.我如何更改該值以便創建新值?

這是腳本

<?php 

// open the file. 

if (($handle = fopen("test2.csv", "r")) !== FALSE) { 
    // read the column headers in an array. 
    $head = fgetcsv($handle, 1000, ";"); 


    // read the actual data. 
    while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) { 



      // create a new array with the elements in $head as keys 
      // and elements in array $data as values. 
      $combined = array_combine($head,$data); 

      // print. 
      var_dump($combined); 
    } 
    // done using the file..close it, 
} 
?> 

這是輸出

陣列(5){[ 「名稱」] =>串(5) 「哈里」[ 「描述」] = > string(4)「test」[「offer_url」] => string(42)「demo.com/admin/offers/add」[「preview_url」] => string(42)「demo.com/admin/offers/添加「[」expiration_date「] => string(9)」23-8-2013「}

array(5){[」name「] => string(5)」Gerry「[」description「] = > string(4)「test」[「offer_url」] =>字符串(42)「demo.com/admin/offers/add」[「preview_url」] =>字符串(42)「demo.com/admin/offers/add」[「expiration_date」] =>字符串(9)「23 -82013「}

array(5){[」name「] => string(5)」merry「[」description「] => string(4)」test「[」offer_url「] =>字符串(42)「demo.com/admin/offers/add」[「preview_url」] =>字符串(42)「demo.com/admin/offers/add」[「expiration_date」] =>字符串(9)「23 -8-2013" }

回答

0

必須經過多維數組,並添加額外的指數組合PARAM這樣的 - 結合[] = ...:

  // create a new array with the elements in $head as keys 
      // and elements in array $data as values. 
      $combined[] = array_combine($head,$data);