0

配列を mysql テーブルに挿入する必要があります。角かっこのある行は、挿入する必要があるものの例です。

Array
(
    [0] => Array
  (
      [batch_id] => 1
      [seq_id] => 1
      [q_id] => 2046
      [a1] => 0
      [a2] => 1
      [a3] => 2
      [a4] => 3
      [a5] => 4

   )

    [1] => Array
 (
     [batch_id] => 1
    [seq_id] => 2
    [q_id] => 2046
    [a1] => 0
    [a2] => 2
    [a3] => 1
    [a4] => 3
    [a5] => 4        
   )

   [2] => Array
 (
     [batch_id] => 1
    [seq_id] => 3
    [q_id] => 2046
    [a1] => 2
    [a2] => 0
    [a3] => 2
    [a4] => 2
    [a5] => 1
   )

テーブル:

 id   |   r_id   | q_id   | c_id  | ranking 
-------------------------------------------
  18965|  2205    | 2046   |   a1  |   0
  18966|  2205    | 2046   |   a2  |   2
  18967|  2205    | 2046   |   a3  |   3
  18968|  2205    | 2046   |   a4  |   1    
  18969|  2205    | 2046   |   a5  |   4
  19965|  2214    | 2046   |   a1  |   0
  19966|  2214    | 2046   |   a2  |   1
  19967|  2214    | 2046   |   a3  |   1
  19968|  2214    | 2046   |   a4  |   2    
  19969|  2214    | 2046   |   a5  |   3
(20965)|  (11)    | (2046) | (a1)  |   (0)
(20966)|  (11)    | (2046) | (a2)  |   (1)
(20967)|  (11)    | (2046) | (a3)  |   (2)
(20968)|  (11)    | (2046) | (a4)  |   (3)   
(20969)|  (11)    | (2046) | (a5)  |   (4)
(21965)|  (12)    | (2046) | (a1)  |   (0)
(21966)|  (12)    | (2046) | (a2)  |   (2)
(21967)|  (12)    | (2046) | (a3)  |   (1)
(21968)|  (12)    | (2046) | (a4)  |   (3)   
(21969)|  (12)    | (2046) | (a5)  |   (4)
(22965)|  (13)    | (2046) | (a1)  |   (2)
(22966)|  (13)    | (2046) | (a2)  |   (0)
(22967)|  (13)    | (2046) | (a3)  |   (2)
(22968)|  (13)    | (2046) | (a4)  |   (2)   
(22969)|  (13)    | (2046) | (a5)  |   (1)

この方法で挿入したい場合は、挿入ステートメントでループを使用していますか? Table.id は自動インクリメントで、r_id は batch_id+seq_id です。

$result = $this->query("Select * from table_3");
$new_insert = array();
   $csvr[] = $this->fetch_array($result);

foreach($csvr as $cr){
   $ir_id = $cr['batch_id'].$cr['seq_id'];

        $q_id = $cr['q_id'];

   $query1 = "insert into t1 (id, survey_id, submitted)     value ('$ir_id', '85',time());";

        $this->query($query1);

          foreach ($cr as $k => $v){

                   if(preg_match('{^a\d+$}',$k)){
         $new_insert[] = array(
                                                        'r_id'=>$ir_id,
                                                        'q_id' =>$q_id,
                                                        'c_id' =>$k,
                                                        'ranking'=>$v
                                        );


$query2 = "insert into Table (r_id, q_id, c_id, ranking) value ('r_id', 'q_id','c_id','ranking')";
 $this->query($query2);
                            }

            }

        }

私はそれを行う方法がわかりません.....誰かが私を助けることができますか?

4

3 に答える 3

1
$row=array();

$row[]=array(
      'batch_id' => 1,
      'seq_id' => 1,
      'q_id' => 2046,
      'a1' => 0,
      'a2' => 1,
      'a3' => 2,
      'a4' => 3,
      'a5' => 4
);
$row[]=array(
      'batch_id' => 2,
      'seq_id' => 2,
      'q_id' => 2047,
      'a1' => 0,
      'a2' => 1,
      'a3' => 2,
      'a4' => 3,
      'a5' => 4
);



 $insert_date =array();

   foreach($row as $r){
       $ir_id = $r['batch_id'].$r['seq_id'];
       $q_id = $r['q_id'];
        foreach($r as $k => $v){

         if(preg_match('{^a\d+$}',$k)){ //update here
                $insert_date[]=array(
                'ir_id'=>$ir_id,
                'q_id'=>$q_id,
                'c_id'=>$k,
                'ranking'=>$v
                );
            }
       }
   }
   print_r($insert_date);

http://sandbox.onlinephpfunctions.com/code/78433f52466d56602eef2b01f668af23f6dd42f4

于 2013-04-29T02:56:08.720 に答える
1

おそらくあなたが探しているinsert...values( http://dev.mysql.com/doc/refman/5.7/en/insert.html ):

INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
于 2013-04-29T02:45:26.713 に答える