dongqu2863 2013-05-13 11:23
浏览 55
已采纳

将合并的数组数据插入mysql表

i have the following table named 'greek'

     ID --  greek -- greeklish

I have the following code that takes text and creates 2 arrays:

        $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);

this results in the following:

        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)

and so on

how i can insert all these values to the database table described above?

thanks

  • 写回答

1条回答 默认 最新

  • dougou6213 2013-05-13 11:35
    关注

    Using PDO you could use the following code:

    //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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题