2016-06-29 3 views
1

Я хочу вставить массив с массивом, как показано ниже, как это сделать, я также попытался вставить пакет, но он не работает.Вставить массив массива в codeigniter

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" 
    } 
} 

Я стараюсь, но это не работает

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

Сообщение об ошибке

<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> 

Это один мой print_r заявление

--- print_r заявление ---

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 
    ) 

    ) 
+0

У вас есть сообщение об ошибке? – Swolschblauw

+0

hi @Swolschblauw нет, сообщений об ошибках нет, но я хочу вставить с этим форматом массива, похоже, что с документами от CI –

+0

hi @Swolschblauw update выше –

ответ

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

так что вы получите массив, как показано ниже, и вы можете использовать его со вставкой партии

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

здесь ключ будет ваше имя столбца таблицы и чем

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

надежды это поможет вам ..

+0

Привет @raj Jagani, да, как переформатировать этот массив чтобы быть похожим на этот формат, это мой вопрос на самом деле. –

+0

ok Я могу это сделать, вы можете просто разместить свой массив с помощью инструкции 'print_r'. –

+0

Привет, заявка на печать будет показана как результат ниже –

0

Вы можете использовать эту функцию для форматирования массива:

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

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

    return $result; 
}