2015-04-17 30 views
1

我試圖在Mysql中保存表單數據。但嵌套數組造成問題。使用活動記錄代碼點燃器在Mysql中保存數組陣列

Array ([name] => Custom Project 
     [number] => 08883 
     [key] => Array ([0] => Server Cost 
         [1] => Domain Cost 
         [2] => Design Charges, 
         ) 
     [value] => Array ([0] => 098 
          [1] => 765 
          [2] => 787 
         ) 
    ) 

我使用的是$this->db->insert('invoice',$array),它插入的數據沒有嵌套數組。

那麼,有沒有一種方法,我可以單獨從上面陣列此嵌套數組,然後保存此嵌套數組到另一個表只有兩列keyvalue

回答

0

我使用了非常簡單的方法,我推一個陣列到另一個,因爲結果是笨插入方法進行處理。

//Item description 
     $description=$_POST['key']; 

     //item amount 
     $amount=$_POST['value']; 

     $big_array=array(); 

     for($i=0; $i<sizeof($description); $i++){ 

       $small_array=array('description'=>$description[$i],'amount'=>$amount[$i]); 

       array_push($big_array,$small_array); 
      } 

     //adding data into model 
     $this->my_Model->InsertData($big_array); 
2

你可以只使用array_combine創建密鑰對的值:

function save_value($array) 
{ 
    $key_value = array_combine($array['key'], $array['value']); 
    $this->db->insert('table_name', $key_value); 
} 
+0

它沒有幫助,但我想出了自己 –