Suresh Mali Suresh Mali - 7 months ago 74
PHP Question

How to merge multiple arrays in Codeigniter

I am trying to merge two arrays, but getting

NULL
. Below is my code

$a = 1;
foreach($codes as $values) {
$id = $values['id'];
$post_data = array (
"id" => $id,
"name" => $this->input->post('Name'),
"from_date" => $this->input->post('FromDate'),
"to_date" => $this->input->post('ToDate')
);
$this->data['output' . $a++] = $this->my_modal->simple_post($post_data);
}

$this->data['output'] = array_merge($this->data['output1'], $this->data['output2']);

var_dump($this->data['output']);


Any suggestions will be appreciated. Thanks..

Answer

You have to delete the first parameter (NULL) of array_merge();

And what is $this->input->$id? Don't you mean $id?

And in this environment, it's better to use array_push();:

$a = 1;
$this->data['output'] = array();
foreach($codes as $values)
{
    $id = $values['id'];
    $post_data = array ( 
    "id" => $id,
    "name" => $this->input->post('Name'),
    "from_date" => $this->input->post('FromDate'),
    "to_date" => $this->input->post('ToDate')
    );

    $new_data = $this->my_modal->simple_post($post_data);
    array_push($this->data['output'], $new_data);
}

var_dump($this->data['output']);