我想插入一个数组如下格式的数组,如何做到这一点,我也尝试插入批处理,但它不起作用。
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 :(得分: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);
希望这会对你有帮助..
答案 1 :(得分:0)
您可以使用此功能格式化阵列:
function toBatchArray($array)
{
$result = array();
foreach ($array as $k => $v)
{
$array = array();
foreach ($v as $value)
{
$array[][$k]= $value;
}
$result[] = $array;
}
return $result;
}