我有两个数组要将数据插入数据库。第一个数组就像
$data= array(
'date'=>$date,
'amount'=>$amt,
);
下一个数组是一样的。但我必须检查一些条件。我把数组放在
if($mode == 1)
{
$data= array(
'Percentage'=>$percent,
'deduction'=>$deduct,
);
}
else if($mode ==2)
{
$data= array(
'Percentage1'=>$percent,
'deduction1'=>$deduct,
);
}
然后我用
$this->amout_model->insert_amount($data,$app_id);
但是插入一个Array的数据。如何插入两个数据数组?
答案 0 :(得分:1)
此代码100%适合您...
$data= array(
'date'=> $date,
'amount'=> $amt,
);
$mode_arr= array();
if($mode == 1)
{
$mode_arr= array(
'Percentage'=>$percent,
'deduction'=>$deduct,
);
}
else if($mode ==2)
{
$mode_arr= array(
'Percentage1'=>$percent,
'deduction1'=>$deduct,
);
}
$data =array_merge($data,$mode_arr);
答案 1 :(得分:1)
您可以尝试以下代码:
$arr1 = array(
'date'=> $date,
'amount'=> $amt,
);
$arr2 = array();
if($mode == 1)
{
$arr2 = array(
'Percentage'=>$percent,
'deduction'=>$deduct,
);
}
else if($mode ==2)
{
$arr2 = array(
'Percentage1'=>$percent,
'deduction1'=>$deduct,
);
}
$temp_arr =array_merge($arr1,$arr2);
现在,$ temp_arr是完整的数组,您可以轻松插入 数据库中。
答案 2 :(得分:-1)
将您将传递的数组名称设为$data
$data1 = array(
'date'=>$date,
'amount'=>$amt,
);
$data2= array(
'Percentage'=>$percent,
'deduction'=>$deduct,
);
$data['data1'] = $data1;
$data['data2'] = $data2;