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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?