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

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像