Php 在codeigniter中插入阵列批次
我想插入一个数组,数组格式如下,如何做,我也尝试过插入批处理,但它不起作用Php 在codeigniter中插入阵列批次,php,arrays,codeigniter,Php,Arrays,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_mess
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>
这里的键将是表的列名,而不是
function toBatchArray($array)
{
$result = array();
foreach ($array as $k => $v)
{
$array = array();
foreach ($v as $value)
{
$array[][$k]= $value;
}
$result[] = $array;
}
return $result;
}
希望这将对您有所帮助。您可以使用此功能格式化阵列:
你有错误消息吗?嗨@Swolschblauw没有,没有错误消息,但我想用数组格式插入,它似乎与CIhi@Swolschblauw更新的文档不同,是的,如何将数组重新格式化为那样的格式,这实际上是我的问题。好的,我可以做到。你可以用
print\r
语句发布你的数组吗?我需要代码将像我上面的问题一样的数组格式转换为你的数组format@AmirRachman:我已更新答案,请从答案中删除您的打印声明。
$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);
function toBatchArray($array)
{
$result = array();
foreach ($array as $k => $v)
{
$array = array();
foreach ($v as $value)
{
$array[][$k]= $value;
}
$result[] = $array;
}
return $result;
}