值如下:
<?php
$imp_name_contact = Array
(
[0] => p1
[1] => +91786555
)
Array
(
[0] => p3
[1] => +9123457788
)
Array
(
[0] => p2
[1] => +91454543
);
for ($i=0; $i < count($imp_name_contact) ; $i++) {
if(empty($imp_name_contact['0'])) {
$imp_name_contact['0'] = '##';
}
$data = array('name' => $imp_name_contact['0'], 'contact' => $imp_name_contact['1'], 'added_date_time' => $currentDateTime, 'user_id' => $user_id );
$query = $this->db->insert("tbl_name", $data);
$this->db->insert_id();
}
?>
但问题是两次插入相同的值。怎么了? 是否可以使用insert_batch?
答案 0 :(得分:2)
你应该以这种方式准备一个数组
$string = 'aabc-+9123457788,fsfs-989776655';
$imp_name_contact = explode(',', $string);
foreach($imp_name_contact as &$i)
$i = explode('-', $i);
$data = [];
foreach($imp_name_contact as $i) {
if(empty($i['0'])) {
$i['0'] = '##';
}
$data[] = array('name' => $i['0'],
'contact' => $i['1'],
'added_date_time' => $currentDateTime,
'user_id' => $user_id );
}
现在您可以使用insert_batch