如何获取下一次迭代的 foreach 循环值 php
how to get the foreach loop value to next iteration php
在我的 foreach 循环中,我有以下代码:
foreach ($qty as $count => $value) {
if ($esc_returnbranch == 1) {
// insert into table1 query
$stock_return_id= $this->db->insert_id();
} else {
// insert into table2 query
$purchase_return_id=$this->db->insert_id();
}
if ($esc_returnbranch == 1) {
//insert into table3 query
} else {
//insert into table4 query
}
}
此处插入 table 1&2 只执行一次,插入 table 3 & 4 需要 table 1 & 2 的插入 ID。
那就是 table 1 & 2 只需要 1 次插入,但 3&4 有多次插入 1& 2 的 ID。
怎么做?
您可以检查您的 ID 值。
$stock_return_id = null;
$purchase_return_id = null;
foreach ($qty as $count => $value) {
if ($stock_return_id == null) {
if ($esc_returnbranch == 1) {
// insert into table1 query
$stock_return_id= $this->db->insert_id();
}
}
if ($purchase_return_id == null) {
if ($esc_returnbranch != 1) {
// insert into table2 query
$purchase_return_id=$this->db->insert_id();
}
}
if ($esc_returnbranch == 1) {
//insert into table3 query
} else {
//insert into table4 query
}
}
在我的 foreach 循环中,我有以下代码:
foreach ($qty as $count => $value) {
if ($esc_returnbranch == 1) {
// insert into table1 query
$stock_return_id= $this->db->insert_id();
} else {
// insert into table2 query
$purchase_return_id=$this->db->insert_id();
}
if ($esc_returnbranch == 1) {
//insert into table3 query
} else {
//insert into table4 query
}
}
此处插入 table 1&2 只执行一次,插入 table 3 & 4 需要 table 1 & 2 的插入 ID。 那就是 table 1 & 2 只需要 1 次插入,但 3&4 有多次插入 1& 2 的 ID。 怎么做?
您可以检查您的 ID 值。
$stock_return_id = null;
$purchase_return_id = null;
foreach ($qty as $count => $value) {
if ($stock_return_id == null) {
if ($esc_returnbranch == 1) {
// insert into table1 query
$stock_return_id= $this->db->insert_id();
}
}
if ($purchase_return_id == null) {
if ($esc_returnbranch != 1) {
// insert into table2 query
$purchase_return_id=$this->db->insert_id();
}
}
if ($esc_returnbranch == 1) {
//insert into table3 query
} else {
//insert into table4 query
}
}