2013-02-27 5 views
0

Je souhaite créer un tableau à partir d'un fichier csv avec l'en-tête comme clé. Mais lorsque j'insère le script dans une base de données, il remplace la ligne précédente du fichier csv et seule la dernière valeur est stockée dans la base de données.csv to array overide data

Je pense que c'est parce que le tableau a une valeur par défaut de 5. Comment puis-je changer la valeur afin qu'elle crée une nouvelle valeur?

C'est le script

<?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, 
} 
?> 

la sortie est la

tableau (5) {[ "nom"] => string (5) "Harry" [ "description"] = > string (4) "test" ["offer_url"] => chaîne (42) "demo.com/admin/offers/add" ["preview_url"] => chaîne (42) "demo.com/admin/offers/ add "[" expiration_date "] => chaîne (9)" 23-8-2013 "}

tableau (5) {[" nom "] => chaîne (5)" Gerry "[" description "] = > string (4) "test" ["offer_url"] => string (42) "demo.com/admin/offers/add" ["preview_url"] => chaîne (42) "demo.com/admin/offers/add" ["expiration_date"] => chaîne (9) "23 -8-2013 "}

tableau (5) {[" nom "] => chaîne (5)" joyeux "[" description "] => chaîne (4)" test "[" offer_url "] => string (42) "demo.com/admin/offers/add" ["preview_url"] => chaîne (42) "demo.com/admin/offers/add" ["expiration_date"] => chaîne (9) "23 -8-2013" }

Répondre

0

vous devez passer par tableau multidimensionnel et ajouter l'index supplémentaire à la combinaison param comme celui-ci - combiné [] = ...:

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