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.

图片转代码服务由CSDN问答提供 功能建议

在我的模型中,有2个函数可以从数据库中获取数据。 两者都将数据作为对象数组返回。下面给出了一个函数。

  public function get_camaramens($ data,$ evdata)
 {
 $ this-> db-  >选择( 'emp_position.employee_id'); 
 $的这 - > DB-肽从( 'emp_position'); 
 $的这 - > DB-化合物其中( 'emp_position.position','C  '); 
 $ this-> db-> where('emp_position.employee_id NOT IN('.implode(“,”,$ data)。')'); 
 $ query = $ this->  db-> get('',$ evdata); 
return $ query-> result(); 
} 
   
 
 

在我的控制器中我接受这个 结果如下。

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

模型中的另一个函数是

  public function get_camara_assistants($ data,$ sdata,$ evdata)
  {
 $ cdata = array(); 
foreach($ sdata ['camaramen_list'] as $ row){
 $ cdata [] = $ row-&gt; employee_id; 
} 
 
 $ this-&gt  ; DB-&GT;选择( 'emp_position.employee_id'); 
 $的这 - &GT; DB-肽从( 'emp_position'); 
 $的这 - &GT; DB-化合物其中('EMP  _position.position','ca'); 
 $ this-&gt; db-&gt; where('emp_position.employee_id NOT IN('.implode(“,”,$ data)。')'); 
 $  this-&gt; db-&gt; where('emp_position.employee_id NOT IN('.implode(“,”,$ cdata)。')'); 
 $ query = $ this-&gt; db-&gt; get(  '',$ evdata); 
return $ query-&gt; result(); 
} 
   
 
 

在我的控制器中我要添加上面的结果 函数到同一个对象数组$ sdata。 但是如果我把如下相同的名称替换为前一个数组。

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

任何人都可以告诉我正确的方法。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

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

相关推荐 更多相似问题