Christeen
Christeen

Reputation: 65

Adding objects to existing array in 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.

Upvotes: 0

Views: 2643

Answers (4)

Nileshsinh Rathod
Nileshsinh Rathod

Reputation: 968

Try this

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

$this->load->view('your_view_page',$sdata['camaramen_list'] );

Upvotes: 0

Gabriel Heming
Gabriel Heming

Reputation: 1165

If both results are arrays, you can use array_merge to merge one or more arrays into a new one.

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

var_dump($sdata['camaramen_list']);//the new array

Upvotes: 0

Benyi
Benyi

Reputation: 952

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.

Upvotes: 0

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26258

Try to understand the basic concept:

$sdata['one'] = 'some value';

and I want to add one more value in that array and I do it like:

$sdata['one'] = 'Some other value';   

// this value overrides the old value, because you are adding the values on same index, this will override already existing value.

So do it like:

$sdata['old']['one'] = 'Some other value'; 

add new value like:

$sdata['new']['one'] = 'Some other value'; 

In this case there are two diff index old, one. So no override is done here.

or save it on some diff index.

Upvotes: 1

Related Questions