在這里,我將資料保存到多行的資料庫中,我想檢索每行的最后一個 id,我該怎么做?
ex : 輸入資料時的 print_r 結果。
Array
(
[0] => Array
(
[name] => metode 11
[description] => DESC 1
)
[1] => Array
(
[name] => metode 22
[description] => DESC 2
)
[2] => Array
(
[name] => metode 33
[description] => DESC 1
)
)
在資料庫中
id | name | description
1 metode 11 desc 1
2 metode 22 desc 2
3 metode 33 desc 3
完成輸入后,我想獲取每個 ID,這是我的代碼
$data = array();
$numrow = 1;
foreach ($sheet as $row) {
if ($numrow > 1) {
array_push($data, array(
'name' => $row['A'],
'description' => $row['B'],
));
}
$numrow ;
}
$this->db->insert_batch('my_tabel', $data);
$myid =$this->db->insert_id();
//after insert i retrieve id using last_id() function, however, only last id is fetched, i want to fetch each id
$data_1 = array();
$numrow2 = 1;
foreach ($sheet as $row) {
if ($numrow2 > 1) {
array_push($data_1, array(
'id_last' => $myid,
'id' => $row['E'],
'barang' => $row['F'],
));
}
$numrow2 ;
}
我想要這樣:
Array
(
[0] => Array
(
[last_id] => 1
[id] => 33
[barang] => ccc
)
[1] => Array
(
[last_id] => 2
[id] => 44
[barang] => dd
)
[2] => Array
(
[last_id] => 3
[id] => 55
[barang] => eee
)
)
提前感謝那些回答我希望我說的足夠清楚
uj5u.com熱心網友回復:
為什么不逐行插入而不是批量插入?從每個插入構建一個字典,以便稍后將其配對。
$data = array();
$numrow = 1;
foreach ($sheet as $row) {
if ($numrow > 1) {
array_push($data, array(
'name' => $row['A'],
'description' => $row['B'],
));
}
$numrow ;
}
$array_of_insert_id = array();
foreach ($data as $index => $data_to_insert) {
$this->db->insert('my_tabel', $data_to_insert);
$array_of_insert_id[$index] = $this->db->insert_id();
}
$data_1 = array();
$numrow2 = 1;
$counter = 0;
foreach ($sheet as $row) {
if ($numrow2 > 1) {
array_push($data_1, array(
'id_last' => $array_of_insert_id[$counter],
'id' => $row['E'],
'barang' => $row['F'],
));
}
$numrow2 ;
$counter ;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/481253.html
標籤:php 数组 代码点火器 codeigniter-3
上一篇:將物件陣列轉換為單個物件
下一篇:拆分指定長度的二維陣列
