-1

I have two arrays to insert data to database. The first array is like

 $data= array(
 'date'=>$date,
 'amount'=>$amt,
 );

Next array is like same. But I have to check some conditions. I put the array like

        if($mode == 1)
        {

           $data= array(
           'Percentage'=>$percent,
           'deduction'=>$deduct,
          );
       }
      else if($mode ==2)
      {
           $data= array(
           'Percentage1'=>$percent,
           'deduction1'=>$deduct,
          );
      }

Then I use

  $this->amout_model->insert_amount($data,$app_id);

But insert the data of one Array. How to insert two array of data?

4
  • can you placed in a loop? Commented Feb 24, 2016 at 7:37
  • Percentage,deduction,Percentage1,deduction1 is diffrent fields in your table Commented Feb 24, 2016 at 7:44
  • yes.different fields Commented Feb 24, 2016 at 7:47
  • test var_dump($data) above the ` $this->amout_model->insert_amount($data,$app_id);` Commented Feb 24, 2016 at 7:50

3 Answers 3

1

This code is 100% works for you...

$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);
Sign up to request clarification or add additional context in comments.

2 Comments

ok.I will try and have some doubts.asku after 20 minz plz help me
yeah sure! any time... :)
1

You can try this code:

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

Now, $temp_arr is complete array which you can insert easily into database.

Comments

-1

Lets the name the array you will pass as $data

 $data1 = array(
 'date'=>$date,
 'amount'=>$amt,
 );

 $data2= array(
 'Percentage'=>$percent,
 'deduction'=>$deduct,
 );

 $data['data1'] = $data1;
 $data['data2'] = $data2;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.