Php 如何在Codeigniter中合并多个阵列
我试图合并两个数组,但得到Php 如何在Codeigniter中合并多个阵列,php,arrays,codeigniter,Php,Arrays,Codeigniter,我试图合并两个数组,但得到NULL。下面是我的代码 $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"
NULL
。下面是我的代码
$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']);
如有任何建议,将不胜感激。谢谢..您必须删除
array_merge()的第一个参数(NULL
)代码>
什么是$this->input->$id
?你是说$id
在这种环境中,最好使用array_push()代码>:
必须删除array_merge()的第一个参数(NULL
)代码>
什么是$this->input->$id
?你是说$id
在这种环境中,最好使用array_push()代码>:
您的代码完全正确,唯一的问题是您以$a=1
启动计数器,然后执行$a++
,这将导致2。因此,output1
不存在。然而,如果你写(注意细微的变化):
您的代码完全正确,唯一的问题是您以$a=1
启动计数器,然后执行$a++
,这将导致2。因此,output1
不存在。然而,如果你写(注意细微的变化):
$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']);
$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')
);
$data['output2']= $this->my_modal->simple_post($post_data);
if(count($this->data['output1']) > 1) {
$this->data['all'] = array_merge($this->data['output1'],$data['output2']);
}else {
$this->data['all'] = $data['output1'];
}
}
print_r($this->data['all']);
$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); // $a = 1 now
$a++; // $a becomes 2 here
}
$this->data['output'] = array_merge($this->data['output1'], $this->data['output2']);
var_dump($this->data['output']);