Php 使用简单阵列的codeigniter中的批更新

Php 使用简单阵列的codeigniter中的批更新,php,arrays,codeigniter,activerecord,Php,Arrays,Codeigniter,Activerecord,我想在Codeigner中执行批处理更新并传递数组数据,而不是运行多个查询 Array ( [439] => 0 [440] => 0 [441] => 0 [442] => 1 [443] => 0 ) 439、440、441、442、443是id,0、0、0、1、0是需要放入活动列的值 我可以通过循环来实现这一点,但我想做一个批量更新 foreach($this->input->post($field_name) as $key => $va

我想在Codeigner中执行批处理更新并传递数组数据,而不是运行多个查询

Array ( [439] => 0 [440] => 0 [441] => 0 [442] => 1 [443] => 0 )
439、440、441、442、443是id,0、0、0、1、0是需要放入活动列的值

我可以通过循环来实现这一点,但我想做一个批量更新

foreach($this->input->post($field_name) as $key => $value)
{
    $insert = array(
        'id'        =>  $key,
        'active'    =>  $value,
    );

    $this->db->where('id', $key)->update($table, array('active' => $value));
}   

使用foreach循环构建数组,然后使用批处理更新

foreach($this->input->post($field_name) as $key => $value)
{
    $insert = array(
        'id'        =>  $key,
        'active'    =>  $value,
    );

    $this->db->where('id', $key)->update($table, array('active' => $value));
}   
编辑:我忘记了开头数组中的第二个
array()。可能需要,也可能不需要

$data = array(array());

foreach($this->input->post($field_name) as $key => $value)
{
    $push = array(
        'id'        =>  $key,
        'active'    =>  $value,
    );
array_push($data, $push);
}
$this->db->update_batch('mytable', $data, 'id');

循环遍历数组并使用关联数组存储数据

foreach($this->input->post($field_name) as $key => $value)
{
  $insert[] = array(
     'id'        =>  $key,
     'active'    =>  $value
  );

}   
$this->db->update_batch('yourtable', $insert, 'id');

这些答案对你有用吗?很有趣。在视图上启用输出探查器时,数据库查询会出现什么结果?