du9698 2016-11-10 10:42
浏览 46
已采纳

在codeigniter中将对象添加到现有数组

In my model there are 2 functions to get data from database. Both return data as arrays of objects.One function is given below.

public function get_camaramens($data,$evdata)
{
$this->db->select('emp_position.employee_id');
$this->db->from('emp_position');
$this->db->where('emp_position.position','c');
$this->db->where('emp_position.employee_id NOT IN ('.implode(",",$data).')');
$query=$this->db->get('',$evdata);
return $query->result();
}

In my controller I accept this result as follows.

$sdata['camaramen_list']    = $this->emp_position_model>get_camaramens($data,$evdata['no_of_cams']);

The other function in the model is

public function get_camara_assistants($data,$sdata,$evdata)
{
$cdata = array();
foreach($sdata['camaramen_list'] as $row) {
$cdata[] = $row->employee_id;
}

$this->db->select('emp_position.employee_id');
$this->db->from('emp_position');
$this->db->where('emp_position.position','ca');
$this->db->where('emp_position.employee_id NOT IN ('.implode(", ",$data).')');
$this->db->where('emp_position.employee_id NOT IN ('.implode(", ",$cdata).')');
$query=$this->db->get('',$evdata);
return $query->result();
}

In my controller I want to add the result of the above function to the same array of objects $sdata. But if I put same name as follows it replace the previous array.

$sdata['camaramen_list']    = $this->emp_position_model->get_camara_assistants($data,$sdata,$evdata['no_of_cams']);

Can anyone tell me correct way please.

  • 写回答

4条回答 默认 最新

  • doucu7525 2016-11-10 11:02
    关注

    If you use the same variable, it could be override. There are two ways you can do that.

    1. array_merge at Controller

      You can merge two arrays by array_merge.

      $result1 = $this->emp_position_model>get_camaramens($data,$evdata['no_of_cams']);
      $result2 = $this->emp_position_model->get_camara_assistants($data,$sdata,$evdata['no_of_cams']);
      
      $combined_result = array_merge($result1, $result2);
      
    2. UNION select at Model

      $this->db->select('emp_position.employee_id');
      $this->db->from('emp_position');
      $this->db->where('emp_position.position','c');
      $this->db->where('emp_position.employee_id NOT IN ('.implode(",",$data).')');
      
      $compiled_1 = $this->db->get_compiled_select();
      
      
      $this->db->select('emp_position.employee_id');
      $this->db->from('emp_position');
      $this->db->where('emp_position.position','ca');
      $this->db->where('emp_position.employee_id NOT IN ('.implode(", ",$data).')');
      $this->db->where('emp_position.employee_id NOT IN ('.implode(", ",$cdata).')');
      
      $compiled_2 = $this->db->get_compiled_select();
      
      
      $query = $this->db->query('( ' .$compiled_2 .' ) UNION ALL ( '. $compiled_1 .' );' );
      

    You can use $sdata['camaramen_list']['one'], $sdata['camaramen_list']['two'] if you wanna create new index. Otherwise, use my way instead.

    Hope it would be help.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码