我试图在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),它不需要嵌套数组就可以很好地插入数据。
那么,我是否可以将这个嵌套数组从上面的数组中分离出来,然后将这个嵌套数组保存到另一个只有两列key和value的表中呢?
发布于 2015-04-18 20:09:45
我使用了非常简单的方法,我将一个数组推入另一个数组,因为结果将在Codeigniter插入方法中处理。
//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);发布于 2015-04-17 14:30:39
您只需使用array_combine创建密钥对值:
function save_value($array)
{
$key_value = array_combine($array['key'], $array['value']);
$this->db->insert('table_name', $key_value);
}https://stackoverflow.com/questions/29701903
复制相似问题