2013-05-13 131 views
-1

我有以下表命名爲「希臘語」插入合併數組數據到MySQL表

 ID -- greek -- greeklish 

我有以下碼取文字,並創建2個數組:

 $str = "Κρίσιμη για το μέλλον των τμημάτων του βόλεϊ είναι η προγραμματισμένη για σήμερα συνεδρίαση του Δ.Σ. στον Ερασιτέχνη Παναθηναϊκό εάν κι εφόσον εμφανιστεί ο πρόεδρος Αχιλλέας Μακρόπουλος ο οποίος για περισσότερο από ένα μήνα δεν έχει επισκεφθεί τα γραφεία του συλλόγου προφανώς ενοχλημένος από τη συμπεριφορά ορισμένων παραγόντων εντός κι εκτός του συλλόγου Στις γυναίκες πρέπει να ληφθούν σημαντικές αποφάσεις για το μέλλον του τμήματος σχετικά με μεταγραφές ενώ μετά την απομάκρυνση του Βασίλη Βλαχονικολού από τα τμήματα υποδομής θα πρέπει να συζητηθεί και να αποφασιστεί η πρόσληψη νέου προπονητή που θα οδηγήσει την Εθνική Κορασίδων στα τελικά του Πανελληνίου πρωταθλήματος Φήμες πάντως λένε πως ο Μακρόπουλος δεν ήταν ενήμερος για την απομάκρυνση του Βλαχονικολού και κυρίως για τους λόγους που έγινε Αποφάσεις περιμένει και το ανδρικό τμήμα σχετικά με την επιβίωση του αν και τα πάντα είναι σε συνάρτηση με την απόφαση του ΑΣΕΑΔ σχετικά με την τιμωρία ή όχι του Ηρακλή Οι ημέρες είναι κρίσιμες για τον Παναθηναϊκό Την ώρα που όλες οι ομάδες αργά αλλά σταθερά προγραμματίζουν το μέλλον τους η διοίκηση του τριφυλλιού βρίσκεται αντιμέτωπη με μία σημαντική πρόκληση καθώς εάν χάσει κι άλλο πολύτιμο χρόνο δεν θα μπορέσει να αντιμετωπίσει τα προβλήματα αλλά και τον ανταγωνισμό"; 
     $str2 = "Krisimh gia to mellon twn tmhmatwn tou volei einai h programmatismenh gia shmera synedriash tou D.S. ston Erasitexnh Panathhnaiko ean ki efoson emfanistei o proedros Axilleas Makropoulos o opoios gia perissotero apo ena mhna den exei episkefthei ta grafeia tou syllogou profanws enoxlhmenos apo th symperifora orismenwn paragontwn entos ki ektos tou syllogou Stis gynaikes prepei na lhfthoyn shmantikes apofaseis gia to mellon tou tmhmatos sxetika me metagrafes enw meta thn apomakrynsh tou Vasilh Vlaxonikoloy apo ta tmhmata ypodomhs tha prepei na syzhththei kai na apofasistei h proslhpsh neou proponhth pou tha odhghsei thn Ethnikh Korasidwn sta telika tou Panellhniou prwtathlhmatos Fhmes pantws lene pws o Makropoulos den htan enhmeros gia thn apomakrynsh tou Vlaxonikoloy kai kyriws gia tous logous pou egine Apofaseis perimenei kai to andriko tmhma sxetika me thn epiviwsh tou an kai ta panta einai se synarthsh me thn apofash tou ASEAD sxetika me thn timwria h oxi tou Hraklh Oi hmeres einai krisimes gia ton Panathhnaiko Thn wra pou oles oi omades arga alla stathera programmatizoun to mellon tous h dioikhsh tou trifyllioy vrisketai antimetwph me mia shmantikh proklhsh kathws ean xasei ki allo polytimo xrono den tha boresei na antimetwpisei ta provlhmata alla kai ton antagwnismo"; 
     $array = explode(' ', $str); 
     $array2 = explode(' ', $str2); 

     function merge(){ 
      //check if there was at least one argument passed. 
      if(func_num_args() > 0){ 
       //get all the arguments 
       $args = func_get_args(); 
       //get the first argument 
       $array = array_shift($args); 
       //check if the first argument is not an array 
       //and if not turn it into one. 
       if(!is_array($array)) $array = array($array); 
       //loop through the rest of the arguments. 
       foreach($args as $array2){ 
        //check if the current argument from the loop 
        //is an array. 
        if(is_array($array2)){ 
         //if so then loop through each value. 
         foreach($array2 as $k=>$v){ 
          //check if that key already exists. 
          if(isset($array[$k])){ 
           //check if that value is already an array. 
           if(is_array($array[$k])){ 
            //if so then add the value to the end 
            //of the array. 
            $array[$k][] = $v; 
           } else { 
            //if not then make it one with the 
            //current value and the new value. 
            $array[$k] = array($array[$k], $v); 
           } 
          } else { 
           //if not exist then add it 
           $array[$k] = $v; 
          } 
         } 
        } else { 
         //if not an array then just add that value to 
         //the end of the array 
         $array[] = $array2; 
        } 
       } 
       //return our array. 
       return($array); 
      } 
      //return false if no values passed. 
      return(false); 
     } 


     $merged = merge($array, $array2); 

     var_dump($merged); 

這導致以下:

 array 
      0 => 
      array 
       0 => string 'Κρίσιμη' (length=14) 
       1 => string 'Krisimh' (length=7) 
      1 => 
      array 
       0 => string 'για' (length=6) 
       1 => string 'gia' (length=3) 
      2 => 
      array 
       0 => string 'το' (length=4) 
       1 => string 'to' (length=2) 
      3 => 
      array 
       0 => string 'μέλλον' (length=12) 
       1 => string 'mellon' (length=6) 
      4 => 
      array 
       0 => string 'των' (length=6) 
       1 => string 'twn' (length=3) 

我如何將所有這些值插入到上述的數據庫表中?

感謝

+0

我不明白...... – worenga 2013-05-13 11:31:13

回答

3

使用PDO,你可以使用下面的代碼:

//Open a PDO connection to the database and store the connection id on $dbh. 
//Adjust database name, username and password 
$dbh = new PDO('mysql:host=localhost;dbname=your_database_name', $user, $pass); 
//Prepare the statement 
$statement = $dbh->prepare("insert into greek(greek, greeklish) values(?, ?)"); 
//Loop over the array and execute the queries 
foreach($merged as $values){ 
    //Note $values should be an array   
    $statement->execute($values); 
} 
+0

優秀!它完成了這項工作! – 2013-05-13 11:39:06

+0

最後一個問題,如果表中存在一個短語,我該如何避免重複? – 2013-05-13 11:40:54

+1

如果您爲該字段定義了「唯一」索引,則使用「插入忽略」將跳過插入重複值。在這裏檢查http://dev.mysql.com/doc/refman/5.1/en/insert.html – Ander2 2013-05-13 11:44:12