1

I want to insert an array with array like below format, how to do this, i've try insert batch also, but it doesn't work.

 array(4) {
  ["notification_title"]=>
  array(2) {
    [0]=>
    string(35) "Hello! We have a good news for you."
    [1]=>
    string(35) "Hello! We have a good news for you."
  }
  ["notification_message"]=>
  array(2) {
    [0]=>
    string(81) "Now you can choose up to three types of advertisers that you wish to collaborate."
    [1]=>
    string(95) "Saat ini Anda sudah dapat memilih maksimal 3 (tiga) tipe iklan untuk dipasang pada mobil Anda. "
  }
  ["notification_type"]=>
  array(2) {
    [0]=>
    string(1) "1"
    [1]=>
    string(1) "1"
  }
  ["notification_language"]=>
  array(2) {
    [0]=>
    string(1) "1"
    [1]=>
    string(1) "2"
  }
}

i've try this, but it doesn't work

public function save($data) {
    $this->db->insert_batch($this->table, $data);   
}

Error Message

<h1>A Database Error Occurred</h1>
    <p>Error Number: 1064</p><p>You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0, 1) VALUES ('Hello! We have a good news for you.','Hello! We have a good news ' at line 1</p><p>INSERT INTO `ads_notification` (0, 1) VALUES ('Hello! We have a good news for you.','Hello! We have a good news for you.'), ('Now you can choose up to three types of advertisers that you wish to collaborate.','Saat ini Anda sudah dapat memilih maksimal 3 (tiga) tipe iklan untuk dipasang pada mobil Anda. '), ('1','1'), ('1','2')</p><p>Filename: C:/xampp/htdocs/movads/application/models/ModelNotifications.php</p><p>Line Number: 127</p>  </div>

This one is my print_r statement

--- Print_r statement ---

    Array
(
    [notification_title] => Array
    (
        [0] => Keep your weekly minimum distance well monitor.
        [1] => Keep your weekly minimum distance well monitor.
    )
[notification_message] => Array
    (
        [0] => We found out that your driving is below weekly minimum distance. Keep driving safely and increase your driving distance to meet the minimum requirements.
        [1] => Anda masih belum memenuhi target jarak minimum mingguan. Tetap menyetir dengan aman dan tingkatkan jarak mingguan Anda. Tetap semangat!
    )

[notification_type] => Array
    (
        [0] => 2
        [1] => 2
    )

[notification_language] => Array
    (
        [0] => 1
        [1] => 2
    )

    )
3
  • Do you have an error message? Commented Jun 29, 2016 at 9:21
  • hi @Swolschblauw no, there is no error messages, but i want to insert with that array format, it seems different with the documentations from CI Commented Jun 29, 2016 at 9:23
  • hi @Swolschblauw update on above Commented Jun 29, 2016 at 9:24

2 Answers 2

2
$post_array = array(
    "notification_title"=>array("Hello! We have a good news for you.","Hello! We have a good news for you."),
    "notification_message"=>array("Now you can choose up to three types of advertisers that you wish to collaborate.","Now you can choose up to three types of advertisers that you wish to collaborate."),
    "notification_type"=>array('1','1'),
    "notification_language"=>array('1','1')
    );
$data = array();
$i = 0;
foreach($post_array as $key=>$val) {
    $i = 0;
    foreach($val as $k=>$v) {
        $data[$i][$key] = $v;
        $i++;
    }
}
echo '<pre>';
print_r($data);

so you will get array like below and you can use it with insert batch

$data = array(
    array(
       'notification_title'=> 'Hello! We have a good news for you',
       'notification_message'=> 'Now you can choose up to three types of advertisers that you wish to collaborate.',
       'notification_type'=>'1',
       'notification_language'=>'1'
    ),
    array(
       'notification_title'=> 'Hello! We have a good news for you',
       'notification_message'=> 'Now you can choose up to three types of advertisers that you wish to collaborate.',
       'notification_type'=>'1',
       'notification_language'=>'1'
    ),
);

here key will be your column name of table and than

$this->db->insert_batch('mytable', $data); 

hope this will help you..

Sign up to request clarification or add additional context in comments.

4 Comments

Hi @raj Jagani, yes, how to reformat that array to be like that format, that is my question actually.
ok I can do that can you just post your array with print_r statement.
i need the code to convert an array format like my questions above to your array format
@AmirRachman : I have updated the answer and please remove your print statement from answer.
0

You can use this function to format your array :

function toBatchArray($array)
{
    $result = array();
    foreach ($array as $k => $v)
    {
        $array = array();

        foreach ($v as $value)
        {
            $array[][$k]= $value;
        }
        $result[] = $array;
    }

    return $result;
}

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.